Skip to contentSkip to conversation

package/urandom-scripts: hash old seed with new seed when saving

PATCH Superseded Jason A. Donenfeld 6 participants 29 messages Mar 23, 2022 – May 22, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 package/urandom-scripts: hash old seed with new seed when saving

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising to you, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

I fixed this in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

---
 package/urandom-scripts/S20urandom | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)
package/urandom-scripts/S20urandom +11 -19
index e4fd125721..f971ea905a 100644
@@ -17,26 +17,16 @@ else
 	pool_size=512
 fi
 
-check_file_size() {
-	[ -f "$URANDOM_SEED" ] || return 1
-	# Try to read two blocks but exactly one will be read if the file has
-	# the correct size.
-	size=$(dd if="$URANDOM_SEED" bs="$pool_size" count=2 2> /dev/null | wc -c)
-	test "$size" -eq "$pool_size"
-}
-
 init_rng() {
-	if check_file_size; then
-		printf 'Initializing random number generator: '
-		dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
-		status=$?
-		if [ "$status" -eq 0 ]; then
-			echo "OK"
-		else
-			echo "FAIL"
-		fi
-		return "$status"
+	printf 'Initializing random number generator: '
+	dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
+	status=$?
+	if [ "$status" -eq 0 ]; then
+		echo "OK"
+	else
+		echo "FAIL"
 	fi
+	return "$status"
 }
 
 save_random_seed() {
@@ -44,7 +34,9 @@ save_random_seed() {
 	if touch "$URANDOM_SEED" 2> /dev/null; then
 		old_umask=$(umask)
 		umask 077
-		dd if=/dev/urandom of="$URANDOM_SEED" bs="$pool_size" count=1 2> /dev/null
+		dd if=/dev/urandom of="$URANDOM_SEED.new" bs="$pool_size" count=1 2> /dev/null
+		cat "$URANDOM_SEED" "$URANDOM_SEED.new" 2>/dev/null | sha256sum | cut -d ' ' -f 1 > "$URANDOM_SEED"
+		rm "$URANDOM_SEED.new"
 		status=$?
 		umask "$old_umask"
 		if [ "$status" -eq 0 ]; then
Signature · 6 lines
2.35.1

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

Hi Christoph, Yann,

I realized I should have CC'd you on the below. Sorry about that.

Jason

Quoted 87 lines · Jason A. Donenfeld

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising to you, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

I fixed this in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

---
 package/urandom-scripts/S20urandom | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)
package/urandom-scripts/S20urandom +11 -20
index e4fd125721..f971ea905a 100644
@@ -17,26 +17,16 @@ else
        pool_size=512
 fi
 
-check_file_size() {
-       [ -f "$URANDOM_SEED" ] || return 1
-       # Try to read two blocks but exactly one will be read if the file has
-       # the correct size.
-       size=$(dd if="$URANDOM_SEED" bs="$pool_size" count=2 2> /dev/null | wc -c)
-       test "$size" -eq "$pool_size"
-}
-
 init_rng() {
-       if check_file_size; then
-               printf 'Initializing random number generator: '
-               dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
-               status=$?
-               if [ "$status" -eq 0 ]; then
-                       echo "OK"
-               else
-                       echo "FAIL"
-               fi
-               return "$status"
+       printf 'Initializing random number generator: '
+       dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
+       status=$?
+       if [ "$status" -eq 0 ]; then
+               echo "OK"
+       else
+               echo "FAIL"
        fi
+       return "$status"
 }
 
 save_random_seed() {
@@ -44,7 +34,9 @@ save_random_seed() {
        if touch "$URANDOM_SEED" 2> /dev/null; then
                old_umask=$(umask)
                umask 077
-               dd if=/dev/urandom of="$URANDOM_SEED" bs="$pool_size" count=1 2> /dev/null
+               dd if=/dev/urandom of="$URANDOM_SEED.new" bs="$pool_size" count=1 2> /dev/null
+               cat "$URANDOM_SEED" "$URANDOM_SEED.new" 2>/dev/null | sha256sum | cut -d ' ' -f 1 > "$URANDOM_SEED"
+               rm "$URANDOM_SEED.new"
                status=$?
                umask "$old_umask"
                if [ "$status" -eq 0 ]; then
--
Quoted 2 lines

2.35.1

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#2 lore raw
In reply to Jason A. Donenfeld
Quoted 54 lines · Jason A. Donenfeld

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising to you, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

I fixed this in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

---
  package/urandom-scripts/S20urandom | 30 +++++++++++-------------------
  1 file changed, 11 insertions(+), 19 deletions(-)
package/urandom-scripts/S20urandom +0 -10
index e4fd125721..f971ea905a 100644
@@ -17,26 +17,16 @@ else
  	pool_size=512
  fi
 
-check_file_size() {
-	[ -f "$URANDOM_SEED" ] || return 1
-	# Try to read two blocks but exactly one will be read if the file has
-	# the correct size.
-	size=$(dd if="$URANDOM_SEED" bs="$pool_size" count=2 2> /dev/null | wc -c)
-	test "$size" -eq "$pool_size"
-}
-
  init_rng() {
-	if check_file_size; then
-		printf 'Initializing random number generator: '

package/urandom-scripts/S20urandom else · line 22

Quoted 10 lines
package/urandom-scripts/S20urandom +2 -8
-		dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
-		status=$?
-		if [ "$status" -eq 0 ]; then
-			echo "OK"
-		else
-			echo "FAIL"
-		fi
-		return "$status"
+	printf 'Initializing random number generator: '
+	dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null

This will fail if the seed does not exist. Either because it's the first boot or because the system is read-only.

The [ -f ] test is still useful in the first case, because init_rng must return 0 for save_random_seed to be called during boot. And if save_random_seed is not called during boot the seed might never be created (you cannot expect stop to be called).

Quoted 11 lines
package/urandom-scripts/S20urandom +6 -0
+	status=$?
+	if [ "$status" -eq 0 ]; then
+		echo "OK"
+	else
+		echo "FAIL"
  	fi
+	return "$status"
  }
 
  save_random_seed() {

package/urandom-scripts/S20urandom save_random_seed() { · line 40

Quoted 8 lines
package/urandom-scripts/S20urandom +3 -1
@@ -44,7 +34,9 @@ save_random_seed() {
  	if touch "$URANDOM_SEED" 2> /dev/null; then
  		old_umask=$(umask)
  		umask 077
-		dd if=/dev/urandom of="$URANDOM_SEED" bs="$pool_size" count=1 2> /dev/null
+		dd if=/dev/urandom of="$URANDOM_SEED.new" bs="$pool_size" count=1 2> /dev/null
+		cat "$URANDOM_SEED" "$URANDOM_SEED.new" 2>/dev/null | sha256sum | cut -d ' ' -f 1 > "$URANDOM_SEED"
+		rm "$URANDOM_SEED.new"
  		status=$?

This breaks the error check. Either these commands must be chained by && or each of them should be tested.

Also, i'm not sure why stderr is supressed for cat. In this case it is not supposed to fail or print anything to stderr.
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#3 lore raw
In reply to Jason A. Donenfeld

Jason, All,

On 2022-03-22 21:52 -0600, Jason A. Donenfeld spake thusly:

Quoted 4 lines

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

Thanks for this patch. I will blindly trust whatever you state about the kernel RNG.

    https://www.zx2c4.com/projects/linux-rng-5.17-5.18/

Still, I have a few comments about this change, see below...

Quoted 2 lines

As a result of this behavior, which may be understandably surprising to you, writing a good seed file into /dev/urandom and then saving a new

s/to you//

(no "personal" address in a commit message)

Quoted 5 lines

seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

I fixed this in systemd with

s/I fixed this/This has been fixed/

(but "we" as you used later is ok)

[--SNIP--]

Quoted 4 lines

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Is there a way to do that within a shell script? If so, would you be kind enough to send a followup patch, please?

package/urandom-scripts/S20urandom

Quoted 9 lines
---
 package/urandom-scripts/S20urandom | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)
package/urandom-scripts/S20urandom +0 -0
index e4fd125721..f971ea905a 100644

[--SNIP--]

package/urandom-scripts/S20urandom save_random_seed() { · line 38

Quoted 7 lines
package/urandom-scripts/S20urandom +2 -1
@@ -44,7 +34,9 @@ save_random_seed() {
 	if touch "$URANDOM_SEED" 2> /dev/null; then
 		old_umask=$(umask)
 		umask 077
-		dd if=/dev/urandom of="$URANDOM_SEED" bs="$pool_size" count=1 2> /dev/null
+		dd if=/dev/urandom of="$URANDOM_SEED.new" bs="$pool_size" count=1 2> /dev/null
+		cat "$URANDOM_SEED" "$URANDOM_SEED.new" 2>/dev/null | sha256sum | cut -d ' ' -f 1 > "$URANDOM_SEED"
    $ shellcheck package/urandom-scripts/S20urandom
    In package/urandom-scripts/S20urandom line 38:
                cat "$URANDOM_SEED" "$URANDOM_SEED.new" 2>/dev/null | sha256sum | cut -d ' ' -f 1 > "$URANDOM_SEED"
                    ^-------------^ SC2094: Make sure not to read and write the same file in the same pipeline.

package/urandom-scripts/S20urandom save_random_seed() { · line 40

Quoted 2 lines
package/urandom-scripts/S20urandom +1 -0
+		rm "$URANDOM_SEED.new"
 		status=$?

This status now only gets the result of the 'rm', which is not very interesting. I guess what we really need is whether the initial 'dd' that extracts from the pool succeeded. If that fails, then we should probably not update the saved seed, should we?

Here's what I suggest instead:

    save_random_seed() {
        printf 'Saving random seed: '
        status=1
        if touch "$URANDOM_SEED" 2> /dev/null; then
            old_umask=$(umask)
            umask 077
            dd if=/dev/urandom of="$URANDOM_SEED.tmp" bs="$pool_size" count=1 2> /dev/null
            new_seed_len="$(wc -c "$URANDOM_SEED.tmp" 2> /dev/null |cut -d ' ' -f 1)"
            if [ "$new_seed_len" -eq "$pool_size" ]; then
                cat "$URANDOM_SEED" "$URANDOM_SEED.tmp" 2>/dev/null \
                | sha256sum \
                | cut -d ' ' -f 1 > "$URANDOM_SEED.new"
                mv "$URANDOM_SEED.new" "$URANDOM_SEED"
                echo "OK"
                status=0
            else
                echo "FAIL"
            fi
            rm -f "$URANDOM_SEED.tmp"
            umask "$old_umask"
        else
            echo "SKIP (read-only file system detected)"
        fi
        return "$status"
    }

What do you think about that? Since this is a bit critical, I did not want to just do that change when applying.

I do note that urandom should always return something, but this script will also need to work on older kernels, sometimes way back to oldish 3.x series, and my recollection of how urandom worked back then is a bit fuzzy. Are we sure it will always have returned enough? If so, then we can ditch the sanity check altogether.

But then, if we failed to read anything from urandom, we'd just hash the old seed, which should not decrease the entropy it had, as the hash has the same size as the seed.

Thoughts?

Regards,
Yann E. MORIN.

Quoted 9 lines
package/urandom-scripts/S20urandom +0 -0
 		umask "$old_umask"
 		if [ "$status" -eq 0 ]; then
Signature · 6 lines
2.35.1

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
Signature · 10 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#4 lore raw
Quoted 6 lines · Yann E. MORIN
Quoted 4 lines

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Is there a way to do that within a shell script? If so, would you be kind enough to send a followup patch, please?

No busybox applet is using RNDADDENTROPY, so it cannot probably be done in a shell script :(

It's also unfortunate that the urandom(4) man page's example script tells people to write to /dev/urandom exactly the way S20urandom does.
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#5 lore raw
In reply to Yann E. MORIN

Hi Yann,

Quoted 6 lines · Yann E. MORIN
Quoted 2 lines

As a result of this behavior, which may be understandably surprising to you, writing a good seed file into /dev/urandom and then saving a new

s/to you//

(no "personal" address in a commit message)

Ack.

Quoted 8 lines
Quoted 6 lines

seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

I fixed this in systemd with

s/I fixed this/This has been fixed/

Ack.

Quoted 7 lines
Quoted 4 lines

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Is there a way to do that within a shell script? If so, would you be kind enough to send a followup patch, please?

No, there isn't, at all. But I'm writing some code now that I hope to release as a standalone utility, and upstream into busybox/util-linux that should make it accessible. When that happens, I'll keep this mailing list informed, and we can move forward then.

Quoted 1 line

Here's what I suggest instead:

Your suggestion looks good, with a few small tweaks that I'll put in v2.

Quoted 9 lines

I do note that urandom should always return something, but this script will also need to work on older kernels, sometimes way back to oldish 3.x series, and my recollection of how urandom worked back then is a bit fuzzy. Are we sure it will always have returned enough? If so, then we can ditch the sanity check altogether.

But then, if we failed to read anything from urandom, we'd just hash the old seed, which should not decrease the entropy it had, as the hash has the same size as the seed.

Since this isn't crediting anything anyway, I don't think the sanity check matters. The randomness might be total junk. It might not. We might get enough. There might be a bug. We don't care, because there's nothing we can do if it fails from shell, and we're not crediting, so it's not a security risk so long as we don't regress.

v2 coming up.

Jason
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#6 lore raw

PATCH v2 1/1 package/urandom-scripts: hash old seed with new seed when saving

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

This has been fixed in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

---
 package/urandom-scripts/S20urandom | 39 +++++++++++++-----------------
 1 file changed, 17 insertions(+), 22 deletions(-)
package/urandom-scripts/S20urandom +17 -22
index e4fd125721..c6b2ebd48f 100644
@@ -17,43 +17,38 @@ else
 	pool_size=512
 fi
 
-check_file_size() {
-	[ -f "$URANDOM_SEED" ] || return 1
-	# Try to read two blocks but exactly one will be read if the file has
-	# the correct size.
-	size=$(dd if="$URANDOM_SEED" bs="$pool_size" count=2 2> /dev/null | wc -c)
-	test "$size" -eq "$pool_size"
-}
-
 init_rng() {
-	if check_file_size; then
-		printf 'Initializing random number generator: '
-		dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
-		status=$?
-		if [ "$status" -eq 0 ]; then
-			echo "OK"
-		else
-			echo "FAIL"
-		fi
-		return "$status"
+	printf 'Initializing random number generator: '
+	dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
+	status=$?
+	if [ "$status" -eq 0 ]; then
+		echo "OK"
+	else
+		echo "FAIL"
 	fi
+	return "$status"
 }
 
 save_random_seed() {
 	printf 'Saving random seed: '
-	if touch "$URANDOM_SEED" 2> /dev/null; then
+	status=1
+	if touch "$URANDOM_SEED.new" 2> /dev/null; then
 		old_umask=$(umask)
 		umask 077
-		dd if=/dev/urandom of="$URANDOM_SEED" bs="$pool_size" count=1 2> /dev/null
-		status=$?
+		dd if=/dev/urandom of="$URANDOM_SEED.tmp" bs="$pool_size" count=1 2> /dev/null
+		cat "$URANDOM_SEED" "$URANDOM_SEED.tmp" 2>/dev/null \
+			| sha256sum \
+			| cut -d ' ' -f 1 > "$URANDOM_SEED.new" && \
+		mv "$URANDOM_SEED.new" "$URANDOM_SEED" && status=0
+		rm -f "$URANDOM_SEED.tmp"
 		umask "$old_umask"
 		if [ "$status" -eq 0 ]; then
 			echo "OK"
 		else
 			echo "FAIL"
 		fi
+
 	else
-		status=$?
 		echo "SKIP (read-only file system detected)"
 	fi
 	return "$status"
Signature · 6 lines
2.35.1

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#7 lore raw

Jason, All,

+Peter: candidate for backporting as a security fix

On 2022-03-23 14:07 -0600, Jason A. Donenfeld spake thusly:

Quoted 25 lines

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

This has been fixed in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

Quoted 6 lines

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Applied to master, thanks.

Regards,
Yann E. MORIN.

Quoted 76 lines
---
 package/urandom-scripts/S20urandom | 39 +++++++++++++-----------------
 1 file changed, 17 insertions(+), 22 deletions(-)
package/urandom-scripts/S20urandom +17 -22
index e4fd125721..c6b2ebd48f 100644
@@ -17,43 +17,38 @@ else
 	pool_size=512
 fi
 
-check_file_size() {
-	[ -f "$URANDOM_SEED" ] || return 1
-	# Try to read two blocks but exactly one will be read if the file has
-	# the correct size.
-	size=$(dd if="$URANDOM_SEED" bs="$pool_size" count=2 2> /dev/null | wc -c)
-	test "$size" -eq "$pool_size"
-}
-
 init_rng() {
-	if check_file_size; then
-		printf 'Initializing random number generator: '
-		dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
-		status=$?
-		if [ "$status" -eq 0 ]; then
-			echo "OK"
-		else
-			echo "FAIL"
-		fi
-		return "$status"
+	printf 'Initializing random number generator: '
+	dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
+	status=$?
+	if [ "$status" -eq 0 ]; then
+		echo "OK"
+	else
+		echo "FAIL"
 	fi
+	return "$status"
 }
 
 save_random_seed() {
 	printf 'Saving random seed: '
-	if touch "$URANDOM_SEED" 2> /dev/null; then
+	status=1
+	if touch "$URANDOM_SEED.new" 2> /dev/null; then
 		old_umask=$(umask)
 		umask 077
-		dd if=/dev/urandom of="$URANDOM_SEED" bs="$pool_size" count=1 2> /dev/null
-		status=$?
+		dd if=/dev/urandom of="$URANDOM_SEED.tmp" bs="$pool_size" count=1 2> /dev/null
+		cat "$URANDOM_SEED" "$URANDOM_SEED.tmp" 2>/dev/null \
+			| sha256sum \
+			| cut -d ' ' -f 1 > "$URANDOM_SEED.new" && \
+		mv "$URANDOM_SEED.new" "$URANDOM_SEED" && status=0
+		rm -f "$URANDOM_SEED.tmp"
 		umask "$old_umask"
 		if [ "$status" -eq 0 ]; then
 			echo "OK"
 		else
 			echo "FAIL"
 		fi
+
 	else
-		status=$?
 		echo "SKIP (read-only file system detected)"
 	fi
 	return "$status"
Signature · 6 lines
2.35.1

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
Signature · 10 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#8 lore raw

From: Yann E. MORIN

Quoted 3 lines

Sent: 24 March 2022 08:25

+Peter: candidate for backporting as a security fix

Probably not - the security fix is the code that actually initialises the RNG.

David

Quoted 121 lines

On 2022-03-23 14:07 -0600, Jason A. Donenfeld spake thusly:

Quoted 25 lines

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

This has been fixed in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

Quoted 6 lines

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Applied to master, thanks.

Regards,
Yann E. MORIN.

Quoted 72 lines
---
 package/urandom-scripts/S20urandom | 39 +++++++++++++-----------------
 1 file changed, 17 insertions(+), 22 deletions(-)
package/urandom-scripts/S20urandom +17 -23
index e4fd125721..c6b2ebd48f 100644
@@ -17,43 +17,38 @@ else
 	pool_size=512
 fi
 
-check_file_size() {
-	[ -f "$URANDOM_SEED" ] || return 1
-	# Try to read two blocks but exactly one will be read if the file has
-	# the correct size.
-	size=$(dd if="$URANDOM_SEED" bs="$pool_size" count=2 2> /dev/null | wc -c)
-	test "$size" -eq "$pool_size"
-}
-
 init_rng() {
-	if check_file_size; then
-		printf 'Initializing random number generator: '
-		dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
-		status=$?
-		if [ "$status" -eq 0 ]; then
-			echo "OK"
-		else
-			echo "FAIL"
-		fi
-		return "$status"
+	printf 'Initializing random number generator: '
+	dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
+	status=$?
+	if [ "$status" -eq 0 ]; then
+		echo "OK"
+	else
+		echo "FAIL"
 	fi
+	return "$status"
 }
 
 save_random_seed() {
 	printf 'Saving random seed: '
-	if touch "$URANDOM_SEED" 2> /dev/null; then
+	status=1
+	if touch "$URANDOM_SEED.new" 2> /dev/null; then
 		old_umask=$(umask)
 		umask 077
-		dd if=/dev/urandom of="$URANDOM_SEED" bs="$pool_size" count=1 2> /dev/null
-		status=$?
+		dd if=/dev/urandom of="$URANDOM_SEED.tmp" bs="$pool_size" count=1 2> /dev/null
+		cat "$URANDOM_SEED" "$URANDOM_SEED.tmp" 2>/dev/null \
+			| sha256sum \
+			| cut -d ' ' -f 1 > "$URANDOM_SEED.new" && \
+		mv "$URANDOM_SEED.new" "$URANDOM_SEED" && status=0
+		rm -f "$URANDOM_SEED.tmp"
 		umask "$old_umask"
 		if [ "$status" -eq 0 ]; then
 			echo "OK"
 		else
 			echo "FAIL"
 		fi
+
 	else
-		status=$?
 		echo "SKIP (read-only file system detected)"
 	fi
 	return "$status"
--

2.35.1

Quoted 4 lines

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

--
.-----------------.--------------------.------------------.--------------------.

|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
Quoted 5 lines

'------------------------------^-------^------------------^--------------------'
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#9 lore raw

David, All,

On 2022-03-24 09:15 +0000, David Laight spake thusly:

Quoted 6 lines

From: Yann E. MORIN

Quoted 3 lines

Sent: 24 March 2022 08:25

+Peter: candidate for backporting as a security fix

Probably not - the security fix is the code that actually initialises the RNG.

Not sure I understood... As Jason explained, we have so far been saving a seed from an RNG that is probably partially deterministic; that is the security issue.

The way we are seeding the RNG fundamentally does not change, because we are not crediting any entropy with whatever we put in there. So, whether we write something, anything, or nothing at all, has no impact on the strength of the RNG.

As far as I understood it, at least.

Regards,
Yann E. MORIN.

Signature · 10 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#10 lore raw

From: Yann E. MORIN

Quoted 8 lines

Sent: 24 March 2022 10:09

David, All,

On 2022-03-24 09:15 +0000, David Laight spake thusly:

Quoted 3 lines

From: Yann E. MORIN

Quoted 2 lines

Sent: 24 March 2022 08:25

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+Peter: candidate for backporting as a security fix

Probably not - the security fix is the code that actually initialises the RNG.

Not sure I understood... As Jason explained, we have so far been saving a seed from an RNG that is probably partially deterministic; that is the security issue.

I'm pretty sure the seed is also saved during shutdown.
So a normal startup doesn't rely on the seed saved by the startup script. (Although is would be better to use a background script
to save after (say) 30 minutes of operation.)

Quoted 4 lines

The way we are seeding the RNG fundamentally does not change, because we are not crediting any entropy with whatever we put in there. So, whether we write something, anything, or nothing at all, has no impact on the strength of the RNG.

Right - the problem is that RNG isn't being given any entropy at boot time.
So it is very weak.
The only point in saving/loading a seed is to give the RNG some entropy at boot time.
Otherwise programs run from startup scripts that read the RNG to get session keys don't get very good values at all. That is the real security problem.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#11 lore raw

David, All,

On 2022-03-24 10:25 +0000, David Laight spake thusly:

Quoted 6 lines

From: Yann E. MORIN

Quoted 5 lines

On 2022-03-24 09:15 +0000, David Laight spake thusly:

Quoted 4 lines

From: Yann E. MORIN

Quoted 1 line

+Peter: candidate for backporting as a security fix

Probably not - the security fix is the code that actually initialises the RNG.

Quoted 7 lines
Quoted 3 lines

Not sure I understood... As Jason explained, we have so far been saving a seed from an RNG that is probably partially deterministic; that is the security issue.

I'm pretty sure the seed is also saved during shutdown.
So a normal startup doesn't rely on the seed saved by the startup script. (Although is would be better to use a background script
to save after (say) 30 minutes of operation.)

The problem is that a lot, if not most, embedded devices are never properly shut-down; instead, they are, intentionally or not, brutally electrically powered off.

In that case, the seed that is saved at boot is what is going to be reused on the next boot. And currently, that seed is weak, because the output of the RNG at boot is mostly predicatble.

Regards,
Yann E. MORIN.

Signature · 10 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 561 099 427 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#12 lore raw

From: Yann E. MORIN

Quoted 19 lines

Sent: 24 March 2022 10:40

David, All,

On 2022-03-24 10:25 +0000, David Laight spake thusly:

Quoted 6 lines

From: Yann E. MORIN

Quoted 5 lines

On 2022-03-24 09:15 +0000, David Laight spake thusly:

Quoted 4 lines

From: Yann E. MORIN

Quoted 1 line

+Peter: candidate for backporting as a security fix

Probably not - the security fix is the code that actually initialises the RNG.

Quoted 7 lines
Quoted 3 lines

Not sure I understood... As Jason explained, we have so far been saving a seed from an RNG that is probably partially deterministic; that is the security issue.

I'm pretty sure the seed is also saved during shutdown.
So a normal startup doesn't rely on the seed saved by the startup script. (Although is would be better to use a background script
to save after (say) 30 minutes of operation.)

Quoted 7 lines

The problem is that a lot, if not most, embedded devices are never properly shut-down; instead, they are, intentionally or not, brutally electrically powered off.

In that case, the seed that is saved at boot is what is going to be reused on the next boot. And currently, that seed is weak, because the output of the RNG at boot is mostly predicatble.

Maybe, but what you are saving at boot is also very weak. Even if the code worked as intended it is almost entirely completely dependant on the previous value.
So you might as well just count the number of times the system has been booted.

There really is little point saving a new block of 'entropy' until the system has had some chance to locate some.

The embedded device's system software also needs to find somewhere non-volatile to save the entropy.
If the system is likely to just get powered off then none of the filesystems are actually likely to be mounted rw. Things like /, /etc, /var and /tmp are very likely to be ramdisk (or ramdisk overlays) to avoid trashing the actual filesystems [1].

So saving the entropy may not be as simple as just writing to a file - the filesystem might need (re)mounting first.

[1] Of course, if you are using a flash disk it might decide to do 'wear levelling' at the time the power is cut. In that case you can lose 'big time'.
I've seen a completely trashed FAT filesystem. Nothing was in the right place at all.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#13 lore raw
In reply to David Laight

Hi David,

Quoted 8 lines · David Laight

From: Yann E. MORIN

Quoted 3 lines

Sent: 24 March 2022 08:25

+Peter: candidate for backporting as a security fix

Probably not - the security fix is the code that actually initialises the RNG.

No. The security fix here is that the old and new seeds are hashed together so that the new one doesn't regress in entropy. Yann is correct.

Jason
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#14 lore raw

From: Jason A. Donenfeld

Quoted 7 lines

Sent: 24 March 2022 13:54

Quoted 4 lines · David Laight

From: Yann E. MORIN

Quoted 2 lines

Sent: 24 March 2022 08:25

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+Peter: candidate for backporting as a security fix

Probably not - the security fix is the code that actually initialises the RNG.

No. The security fix here is that the old and new seeds are hashed together so that the new one doesn't regress in entropy. Yann is correct.

But there is no entropy, you've never let the system collect any.

It is still completely broken.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#15 lore raw

Hi David,

Quoted 14 lines · David Laight

From: Jason A. Donenfeld

Quoted 12 lines

Sent: 24 March 2022 13:54

Quoted 8 lines · David Laight

From: Yann E. MORIN

Quoted 3 lines

Sent: 24 March 2022 08:25

+Peter: candidate for backporting as a security fix

Probably not - the security fix is the code that actually initialises the RNG.

Quoted 7 lines
Quoted 3 lines

No. The security fix here is that the old and new seeds are hashed together so that the new one doesn't regress in entropy. Yann is correct.

But there is no entropy, you've never let the system collect any.

It is still completely broken.

You misunderstand the problem. An initial seed might be derived from some external source and be high quality. The goal is for the next seed, generated on-device, to be not lower quality. Prior to this patch, it might become lower quality. With this patch, it now doesn't regress in quality.

Jason
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#16 lore raw
In reply to Yann E. MORIN
Quoted 31 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Yann" == Yann E MORIN <[email protected]> writes:

Jason, All,
+Peter: candidate for backporting as a security fix

On 2022-03-23 14:07 -0600, Jason A. Donenfeld spake thusly:

Quoted 25 lines

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

This has been fixed in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

Quoted 8 lines
Quoted 6 lines

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Applied to master, thanks.

Committed to 2021.02.x and 2022.02.x, thanks.

Signature · 5 lines
Bye, Peter Korsgaard
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#17 lore raw
Quoted 36 lines · Peter Korsgaard
Quoted 36 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Yann" == Yann E MORIN <[email protected]> writes:

Jason, All,
+Peter: candidate for backporting as a security fix

On 2022-03-23 14:07 -0600, Jason A. Donenfeld spake thusly:

Quoted 30 lines

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

This has been fixed in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Applied to master, thanks.

Committed to 2021.02.x and 2022.02.x, thanks.

Hello Jason,

I noticed that you worked a lot on the S20urandom init script for sysvinit in buildroot.

I recently updated from 2022.02 release to 2022.02.1 and now on booting all our boards, I noticed this message:

"Initializing random number generator: FAIL"

With the 2022.02 release, with the same kernel, the message stated "OK".

So it might be something changed on this BR revision that causes this fail.

As you worked more on these scripts , do you have maybe an idea or a hint about what I can try , to have it working as before ? Or maybe what change can cause it ?

I tried to test 2022.02 + this patch I am replying to, and things appear to work fine. So it must be something else then...

Thank you for your help !

Eugen

Quoted 8 lines

--
Bye, Peter Korsgaard
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#18 lore raw
Quoted 58 lines · Eugen.Hristev--- via buildroot
Quoted 36 lines · Peter Korsgaard
Quoted 36 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Yann" == Yann E MORIN <[email protected]> writes:

Jason, All,
+Peter: candidate for backporting as a security fix

On 2022-03-23 14:07 -0600, Jason A. Donenfeld spake thusly:

Quoted 30 lines

Writing into /dev/urandom doesn't actually credit any entropy bits. And while it adds that data to the entropy pool, it won't actually be immediately used when reading from /dev/urandom subsequently. This is how the kernel's /dev/urandom has always worked, unfortunately.

As a result of this behavior, which may be understandably surprising, writing a good seed file into /dev/urandom and then saving a new seed file immediately after is dangerous, because the new seed file may wind up being entirely deterministic, even if the old seed file was quite good.

This has been fixed in systemd with
<https://github.com/systemd/systemd/commit/da2862ef06f22fc8d31dafced6d2d6dc14f2ee0b>,
and fortunately it's possible to do the same thing in shell script here. Specifically, instead of just saving new /dev/urandom output straight up, we hash the new /dev/urandom together with the old seed, in order to produce the new seed. This way the amount of entropy in the new seed will stay the same or get better, but not appreciably regress.

At the same time, the pool size check in this script is useless. Writing to /dev/urandom never credits bits anyway, so no matter what, writing into /dev/urandom is useful and not harmful. There's also not much of a point in seeding with more than 256 bits, which is what the hashing operation above produces. So this commit removes the file size check.

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Applied to master, thanks.

Committed to 2021.02.x and 2022.02.x, thanks.

Hello Jason,

I noticed that you worked a lot on the S20urandom init script for sysvinit in buildroot.

I recently updated from 2022.02 release to 2022.02.1 and now on booting all our boards, I noticed this message:

"Initializing random number generator: FAIL"

With the 2022.02 release, with the same kernel, the message stated "OK".

So it might be something changed on this BR revision that causes this fail.

Quoted 8 lines

As you worked more on these scripts , do you have maybe an idea or a hint about what I can try , to have it working as before ? Or maybe what change can cause it ?

I tried to test 2022.02 + this patch I am replying to, and things appear to work fine. So it must be something else then...

Thank you for your help !

If /var/lib/random-seed does not already exist during boot then the new version of the script will fail and not create it, because start is "init_rng && save_random_seed" and init_rng will fail. If S20urandom stop is never called then this may explain the issue.

I pointed this out before but it must have been lost:

https://lore.kernel.org/all/20220324103939.GD3649946@scaer/T/#md433db1bd1e08f3476eefd4e9a83ead5e12b1276

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#19 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Nicolas" == Nicolas Cavallari <[email protected]> writes:

Hi,

Quoted 11 lines
Quoted 2 lines

to work fine. So it must be something else then... Thank you for your help !

If /var/lib/random-seed does not already exist during boot then the new version of the script will fail and not create it, because start is "init_rng && save_random_seed" and init_rng will fail. If S20urandom stop is never called then this may explain the issue.

I pointed this out before but it must have been lost:

https://lore.kernel.org/all/20220324103939.GD3649946@scaer/T/#md433db1bd1e08f3476eefd4e9a83ead5e12b1276

Ahh, indeed. Care to send a patch to fix it?

Signature · 5 lines
Bye, Peter Korsgaard
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#20 lore raw

PATCH 1/1 package/urandom-scripts: do not seed if initial seed doesn't exist

By returning a failure in the event that the initial seed doesn't exist, we'd then skip creating a new seed, which means we'd never in fact have an initial seed, and this script is therefore useless. Fix this by checking for the existence of the seed file first, and just returning 0 if it's not there.

---
 package/urandom-scripts/S20urandom | 1 +
 1 file changed, 1 insertion(+)
package/urandom-scripts/S20urandom +1 -0
index c6b2ebd48f..6c6aea9eee 100644
@@ -18,6 +18,7 @@ else
 fi
 
 init_rng() {
+	[ -f "$URANDOM_SEED" ] || return 0
 	printf 'Initializing random number generator: '
 	dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
 	status=$?
Signature · 6 lines
2.35.1

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#21 lore raw

Re: [Buildroot] [PATCH] package/urandom-scripts: do not seed if initial seed doesn't exist

Quoted 11 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jason" == Jason A Donenfeld <[email protected]> writes:

By returning a failure in the event that the initial seed doesn't exist, we'd then skip creating a new seed, which means we'd never in fact have an initial seed, and this script is therefore useless. Fix this by checking for the existence of the seed file first, and just returning 0 if it's not there.

Committed, thanks.

Signature · 5 lines
Bye, Peter Korsgaard
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#22 lore raw

Re: [Buildroot] [PATCH] package/urandom-scripts: do not seed if initial seed doesn't exist

Quoted 8 lines · Peter Korsgaard
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jason" == Jason A Donenfeld <[email protected]> writes:

By returning a failure in the event that the initial seed doesn't exist, we'd then skip creating a new seed, which means we'd never in fact have an initial seed, and this script is therefore useless. Fix this by checking for the existence of the seed file first, and just returning 0 if it's not there.

Quoted 9 lines
Quoted 3 lines

Committed, thanks.

--
Bye, Peter Korsgaard

Hi Jason,

I cherry-picked this patch to my 2022.02.1 tree, and rebuilt my images. I still see this message at boot time :

"Initializing random number generator: FAIL"

Any ideas about what I could try ?

Thanks,
Eugen
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#23 lore raw

Re: [Buildroot] [PATCH] package/urandom-scripts: do not seed if initial seed doesn't exist

Hi Eugen,

Quoted 21 lines · Eugen.Hristev--- via buildroot
Quoted 8 lines · Peter Korsgaard
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jason" == Jason A Donenfeld <[email protected]> writes:

By returning a failure in the event that the initial seed doesn't exist, we'd then skip creating a new seed, which means we'd never in fact have an initial seed, and this script is therefore useless. Fix this by checking for the existence of the seed file first, and just returning 0 if it's not there.

Quoted 9 lines
Quoted 3 lines

Committed, thanks.

--
Bye, Peter Korsgaard

Quoted 8 lines

Hi Jason,

I cherry-picked this patch to my 2022.02.1 tree, and rebuilt my images. I still see this message at boot time :

"Initializing random number generator: FAIL"

Any ideas about what I could try ?

Huh, no, not sure. Are you sure your new images are running the new file? I've manually run that function and I don't see it erroring...

Jason
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#24 lore raw

Re: [Buildroot] [PATCH] package/urandom-scripts: do not seed if initial seed doesn't exist

Quoted 29 lines · Jason A. Donenfeld

Hi Eugen,

Quoted 26 lines · Eugen.Hristev--- via buildroot
Quoted 8 lines · Peter Korsgaard
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jason" == Jason A Donenfeld <[email protected]> writes:

By returning a failure in the event that the initial seed doesn't exist, we'd then skip creating a new seed, which means we'd never in fact have an initial seed, and this script is therefore useless. Fix this by checking for the existence of the seed file first, and just returning 0 if it's not there.

Quoted 9 lines
Quoted 3 lines

Committed, thanks.

--
Bye, Peter Korsgaard

Hi Jason,

I cherry-picked this patch to my 2022.02.1 tree, and rebuilt my images. I still see this message at boot time :

Quoted 9 lines
Quoted 3 lines

"Initializing random number generator: FAIL"

Any ideas about what I could try ?

Huh, no, not sure. Are you sure your new images are running the new file? I've manually run that function and I don't see it erroring...

Jason

I apologize, it looks like I committed the patch to the wrong branch. It's working fine now, reported OK on the message.

Thanks for helping out !

Eugen
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#25 lore raw

Re: [Buildroot] [PATCH] package/urandom-scripts: do not seed if initial seed doesn't exist

In reply to Eugen.Hristev--- via buildroot
Quoted 22 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]> writes:

Quoted 8 lines · Peter Korsgaard
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jason" == Jason A Donenfeld <[email protected]> writes:

By returning a failure in the event that the initial seed doesn't exist, we'd then skip creating a new seed, which means we'd never in fact have an initial seed, and this script is therefore useless. Fix this by checking for the existence of the seed file first, and just returning 0 if it's not there.

Quoted 9 lines
Quoted 3 lines

Committed, thanks.

--
Bye, Peter Korsgaard

Quoted 8 lines

Hi Jason,

I cherry-picked this patch to my 2022.02.1 tree, and rebuilt my images. I still see this message at boot time :

"Initializing random number generator: FAIL"

Any ideas about what I could try ?

Not really. If you see the message, then that must mean that /var/lib/random-seed exists in your rootfs. Can you provide some more details about your setup, E.G. what kind of rootfs file system, is it mounted R/W, what platform, kernel version?

What error message do you get from dd if you run the dd to /dev/urandom statement manually?

Signature · 5 lines
Bye, Peter Korsgaard
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#26 lore raw

Re: [Buildroot] [PATCH] package/urandom-scripts: do not seed if initial seed doesn't exist

In reply to Jason A. Donenfeld
Quoted 11 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jason" == Jason A Donenfeld <[email protected]> writes:

By returning a failure in the event that the initial seed doesn't exist, we'd then skip creating a new seed, which means we'd never in fact have an initial seed, and this script is therefore useless. Fix this by checking for the existence of the seed file first, and just returning 0 if it's not there.

Committed to 2022.02.x, thanks.

Quoted 19 lines
---
 package/urandom-scripts/S20urandom | 1 +
 1 file changed, 1 insertion(+)
package/urandom-scripts/S20urandom +1 -0
index c6b2ebd48f..6c6aea9eee 100644
@@ -18,6 +18,7 @@ else
 fi
 
 init_rng() {
+	[ -f "$URANDOM_SEED" ] || return 0
 	printf 'Initializing random number generator: '
 	dd if="$URANDOM_SEED" bs="$pool_size" of=/dev/urandom count=1 2> /dev/null
 	status=$?
Signature · 1 line
2.35.1
Signature · 5 lines
Bye, Peter Korsgaard
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#27 lore raw
In reply to Eugen.Hristev--- via buildroot
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

<[email protected]> writes:

Hi,

Quoted 7 lines
Quoted 1 line

Committed to 2021.02.x and 2022.02.x, thanks.

Hello Jason,

I noticed that you worked a lot on the S20urandom init script for sysvinit in buildroot.

Quoted 6 lines

I recently updated from 2022.02 release to 2022.02.1 and now on booting all our boards, I noticed this message:

"Initializing random number generator: FAIL"

With the 2022.02 release, with the same kernel, the message stated "OK".

This comes from an error writing the seed value to /dev/urandom:

https://git.buildroot.org/buildroot/tree/package/urandom-scripts/S20urandom#n22

Given that you state that it displayed OK in 2022.02, I take it that you have a writable and persistent /var/lib, so /var/lib/urandom-seed is present?

Maybe just try doing that by hand (without the 2>/dev/null) and see what error message dd displays?

Signature · 5 lines
Bye, Peter Korsgaard
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#28 lore raw
In reply to Jason A. Donenfeld

Hi Yann,

Quoted 5 lines · Jason A. Donenfeld
Quoted 5 lines
Quoted 4 lines

As a final note, while this commit improves upon the status quo by removing a vulnerability, this shell script still does not actually initialize the RNG like it says it does. For initialization via a seed file, the RNDADDENTROPY ioctl must be used.

Quoted 7 lines
Quoted 2 lines

Is there a way to do that within a shell script? If so, would you be kind enough to send a followup patch, please?

No, there isn't, at all. But I'm writing some code now that I hope to release as a standalone utility, and upstream into busybox/util-linux that should make it accessible. When that happens, I'll keep this mailing list informed, and we can move forward then.

Initial draft is here:

https://git.zx2c4.com/seedrng/about/
https://git.zx2c4.com/seedrng/tree/seedrng.c

I'll probably play with that for a few more days to make sure it's solid. Probably the long term solution here is putting something like that into busybox, and having scripts call that instead.

Jason
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot