Skip to contentSkip to conversation

syscalls/getrandom02: lower bufsize if low on entropy

PATCH New Jan Stancek 3 participants 7 messages Feb 5, 2020 – Feb 7, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/getrandom02: lower bufsize if low on entropy

Some PPC KVM guests accumulate about 1 bit of entropy per second while idle and running getrandom02. Which isn't enough and test sporadically fails on timeout.

Adjust the buffer size by looking at entropy_avail. We want to run the test, even if all entropy is exhausted, but with smaller buffer we don't set as high expectations on how much entropy is generated within default test time.

---
 testcases/kernel/syscalls/getrandom/getrandom02.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/getrandom/getrandom02.c +10 -2
index ee0193df9897..1384fc5f32c0 100644
@@ -10,6 +10,8 @@
 #include "lapi/syscalls.h"
 #include "tst_test.h"
 
+#define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
+
 static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
 		       GRND_RANDOM | GRND_NONBLOCK };
 
@@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
 static void verify_getrandom(unsigned int n)
 {
 	unsigned char buf[256];
+	int bufsize = 64, entropy_avail;
 
-	memset(buf, 0, sizeof(buf));
+	if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
+		SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
+		if (entropy_avail > 256)
+			bufsize = sizeof(buf);
+	}
 
+	memset(buf, 0, sizeof(buf));
 	do {
-		TEST(tst_syscall(__NR_getrandom, buf, sizeof(buf), modes[n]));
+		TEST(tst_syscall(__NR_getrandom, buf, bufsize, modes[n]));
 	} while ((modes[n] & GRND_NONBLOCK) && TST_RET == -1
 		  && TST_ERR == EAGAIN);
 
Signature · 1 line
2.18.1
#1 lore raw

PATCH 1/1 syscalls/getrandom02: lower bufsize if low on entropy

Quoted 30 lines · Jan Stancek

Some PPC KVM guests accumulate about 1 bit of entropy per second while idle and running getrandom02. Which isn't enough and test sporadically fails on timeout.

Adjust the buffer size by looking at entropy_avail. We want to run the test,
even if all entropy is exhausted, but with smaller buffer we don't set as high expectations on how much entropy is generated within default test time.

---
 testcases/kernel/syscalls/getrandom/getrandom02.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/getrandom/getrandom02.c +2 -0
index ee0193df9897..1384fc5f32c0 100644
@@ -10,6 +10,8 @@
 #include "lapi/syscalls.h"
 #include "tst_test.h"
 
+#define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
+
 static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
                       GRND_RANDOM | GRND_NONBLOCK };
 

testcases/kernel/syscalls/getrandom/getrandom02.c static int check_content(unsigned char *buf, int nb) · line 43

Quoted 5 lines
testcases/kernel/syscalls/getrandom/getrandom02.c +1 -0
@@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
 static void verify_getrandom(unsigned int n)
 {
        unsigned char buf[256];
+       int bufsize = 64, entropy_avail;
 

I'm not sure why here initialize bufsize as 64? can you explain more?

Quoted 24 lines
testcases/kernel/syscalls/getrandom/getrandom02.c +7 -3
-       memset(buf, 0, sizeof(buf));
+       if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
+               SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
+               if (entropy_avail > 256)
+                       bufsize = sizeof(buf);
+       }
 
+       memset(buf, 0, sizeof(buf));
        do {
-               TEST(tst_syscall(__NR_getrandom, buf, sizeof(buf),
 modes[n]));
+               TEST(tst_syscall(__NR_getrandom, buf, bufsize, modes[n]));
        } while ((modes[n] & GRND_NONBLOCK) && TST_RET == -1
                  && TST_ERR == EAGAIN);
 
--

2.18.1

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200205/5da9c352/attachment-0001.htm>
#2 lore raw

PATCH 1/1 syscalls/getrandom02: lower bufsize if low on entropy

Quoted 34 lines · Li Wang
Quoted 30 lines · Jan Stancek

Some PPC KVM guests accumulate about 1 bit of entropy per second while idle
and running getrandom02. Which isn't enough and test sporadically fails on timeout.

Adjust the buffer size by looking at entropy_avail. We want to run the test,
even if all entropy is exhausted, but with smaller buffer we don't set as high expectations on how much entropy is generated within default test time.

---
 testcases/kernel/syscalls/getrandom/getrandom02.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/getrandom/getrandom02.c +2 -0
index ee0193df9897..1384fc5f32c0 100644
@@ -10,6 +10,8 @@
 #include "lapi/syscalls.h"
 #include "tst_test.h"
 
+#define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
+
 static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
                       GRND_RANDOM | GRND_NONBLOCK };
 
Quoted 9 lines

testcases/kernel/syscalls/getrandom/getrandom02.c static int check_content(unsigned char *buf, int nb) · line 43

Quoted 6 lines
testcases/kernel/syscalls/getrandom/getrandom02.c +1 -0
@@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
 static void verify_getrandom(unsigned int n)
 {
        unsigned char buf[256];
+       int bufsize = 64, entropy_avail;
 

I'm not sure why here initialize bufsize as 64? can you explain more?

Ah I see, it just picks a smaller size to obtain littler random bytes to decrease the failure probability. But not has special meaning for 64, right?

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200205/845b3877/attachment.htm>
#3 lore raw

PATCH 1/1 syscalls/getrandom02: lower bufsize if low on entropy

In reply to Li Wang

----- Original Message -----

Quoted 9 lines

testcases/kernel/syscalls/getrandom/getrandom02.c static int check_content(unsigned char *buf, int nb) · line 43

Quoted 7 lines
testcases/kernel/syscalls/getrandom/getrandom02.c +1 -0
@@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
 static void verify_getrandom(unsigned int n)
 {
        unsigned char buf[256];
+       int bufsize = 64, entropy_avail;
 

I'm not sure why here initialize bufsize as 64? can you explain more?

That would be the default, unless we know we have enough entropy.

I picked 64 as it matches 'random_read_wakeup_bits'. Assuming we can get 1bit/s entropy, then potentially worst case of waiting 4x for 64bit, it should still fit within default 300 seconds.

But it's also rule of thumb, because it's smaller.

Quoted 31 lines
Quoted 22 lines
testcases/kernel/syscalls/getrandom/getrandom02.c +7 -3
-       memset(buf, 0, sizeof(buf));
+       if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
+               SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
+               if (entropy_avail > 256)
+                       bufsize = sizeof(buf);
+       }
 
+       memset(buf, 0, sizeof(buf));
        do {
-               TEST(tst_syscall(__NR_getrandom, buf, sizeof(buf),
 modes[n]));
+               TEST(tst_syscall(__NR_getrandom, buf, bufsize, modes[n]));
        } while ((modes[n] & GRND_NONBLOCK) && TST_RET == -1
                  && TST_ERR == EAGAIN);
 
--

2.18.1

Quoted 4 lines

--
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Regards,
Li Wang

#4 lore raw

PATCH 1/1 syscalls/getrandom02: lower bufsize if low on entropy

Quoted 12 lines · Jan Stancek

----- Original Message -----

Quoted 2 lines

Review static int check_content(unsigned char *buf, int

Quoted 2 lines
(quoted diff) +0 -0
@@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int

nb)

Quoted 6 lines
Quoted 5 lines
testcases/kernel/syscalls/getrandom/getrandom02.c +1 -0
 static void verify_getrandom(unsigned int n)
 {
        unsigned char buf[256];
+       int bufsize = 64, entropy_avail;
 
Quoted 8 lines
Quoted 1 line

I'm not sure why here initialize bufsize as 64? can you explain more?

That would be the default, unless we know we have enough entropy.

I picked 64 as it matches 'random_read_wakeup_bits'. Assuming we can get 1bit/s entropy, then potentially worst case of waiting 4x for 64bit, it should still fit within default 300 seconds.

That sounds reasonable. Thanks for the analysis.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200206/521ada62/attachment.htm>
#5 lore raw

PATCH 1/1 syscalls/getrandom02: lower bufsize if low on entropy

In reply to Jan Stancek

Hi!

Quoted 28 lines

Some PPC KVM guests accumulate about 1 bit of entropy per second while idle and running getrandom02. Which isn't enough and test sporadically fails on timeout.

Adjust the buffer size by looking at entropy_avail. We want to run the test, even if all entropy is exhausted, but with smaller buffer we don't set as high expectations on how much entropy is generated within default test time.

---
 testcases/kernel/syscalls/getrandom/getrandom02.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/getrandom/getrandom02.c +2 -0
index ee0193df9897..1384fc5f32c0 100644
@@ -10,6 +10,8 @@
 #include "lapi/syscalls.h"
 #include "tst_test.h"
 
+#define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
+
 static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
 		       GRND_RANDOM | GRND_NONBLOCK };
 
@@ -37,11 +39,17 @@ static int check_content(unsigned char *buf, int nb)
 static void verify_getrandom(unsigned int n)

testcases/kernel/syscalls/getrandom/getrandom02.c static int check_content(unsigned char *buf, int nb) · line 48

Quoted 10 lines
testcases/kernel/syscalls/getrandom/getrandom02.c +6 -1
 {
 	unsigned char buf[256];
+	int bufsize = 64, entropy_avail;
 
-	memset(buf, 0, sizeof(buf));
+	if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
+		SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
+		if (entropy_avail > 256)
+			bufsize = sizeof(buf);
+	}

Looks good to me, acked.

Signature · 2 lines
Cyril Hrubis
[email protected]
#6 lore raw

PATCH 1/1 syscalls/getrandom02: lower bufsize if low on entropy

----- Original Message -----

Quoted 2 lines

Looks good to me, acked.

Thanks, pushed.