From 8ea9dd2c5524b8bda209daf0050da00915c3c02d Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 12 Mar 2024 05:35:00 +0100 Subject: [PATCH] patches/finit: backport random seed and rtc fixes - rtc, already does the best it can, fix occasional duped message - urandom, check if seed file empty, try using hwrng Signed-off-by: Joachim Wiberg --- ...-retry-don-t-print-Restoring-RTC-twi.patch | 29 +++++++ ...ort-for-hwrng-and-check-if-seed-file.patch | 81 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 patches/finit/4.7/0005-plugins-on-error-retry-don-t-print-Restoring-RTC-twi.patch create mode 100644 patches/finit/4.7/0006-plugins-add-support-for-hwrng-and-check-if-seed-file.patch diff --git a/patches/finit/4.7/0005-plugins-on-error-retry-don-t-print-Restoring-RTC-twi.patch b/patches/finit/4.7/0005-plugins-on-error-retry-don-t-print-Restoring-RTC-twi.patch new file mode 100644 index 00000000..ceef8ac2 --- /dev/null +++ b/patches/finit/4.7/0005-plugins-on-error-retry-don-t-print-Restoring-RTC-twi.patch @@ -0,0 +1,29 @@ +From 90a4df8d1281fd64d048ec1bc91e8cec5f96df06 Mon Sep 17 00:00:00 2001 +From: Joachim Wiberg +Date: Mon, 11 Mar 2024 18:53:22 +0100 +Subject: [PATCH 5/6] plugins: on error-retry, don't print "Restoring RTC" + twice +Organization: Addiva Elektronik + +Signed-off-by: Joachim Wiberg +--- + plugins/rtc.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/plugins/rtc.c b/plugins/rtc.c +index 31c99e0..238791f 100644 +--- a/plugins/rtc.c ++++ b/plugins/rtc.c +@@ -180,7 +180,8 @@ static void rtc_restore(void *arg) + rc = 2; + } + +- print_desc(NULL, "Restoring system clock (UTC) from RTC"); ++ if (!rc) ++ print_desc(NULL, "Restoring system clock (UTC) from RTC"); + tm.tm_isdst = -1; /* Use tzdata to figure it out, please. */ + tv.tv_sec = mktime(&tm); + if (tv.tv_sec == (time_t)-1 || tv.tv_sec < rtc_date_fallback) { +-- +2.34.1 + diff --git a/patches/finit/4.7/0006-plugins-add-support-for-hwrng-and-check-if-seed-file.patch b/patches/finit/4.7/0006-plugins-add-support-for-hwrng-and-check-if-seed-file.patch new file mode 100644 index 00000000..5f284a13 --- /dev/null +++ b/patches/finit/4.7/0006-plugins-add-support-for-hwrng-and-check-if-seed-file.patch @@ -0,0 +1,81 @@ +From 49218a66e8eae8db1e285bbf8b55ac7109fa7c6c Mon Sep 17 00:00:00 2001 +From: Joachim Wiberg +Date: Tue, 12 Mar 2024 05:18:35 +0100 +Subject: [PATCH 6/6] plugins: add support for hwrng and check if seed file is + empty +Organization: Addiva Elektronik + +Signed-off-by: Joachim Wiberg +--- + plugins/urandom.c | 41 ++++++++++++++++++++++++++++++----------- + 1 file changed, 30 insertions(+), 11 deletions(-) + +diff --git a/plugins/urandom.c b/plugins/urandom.c +index 64a3fa7..0f4ec0c 100644 +--- a/plugins/urandom.c ++++ b/plugins/urandom.c +@@ -47,6 +47,7 @@ static void setup(void *arg) + #ifdef RANDOMSEED + struct rand_pool_info *rpi; + ssize_t len = 0; ++ struct stat st; + int rc = -1; + int fd, err; + +@@ -55,7 +56,7 @@ static void setup(void *arg) + return; + } + +- if (!fexist(RANDOMSEED)) { ++ if (!stat(RANDOMSEED, &st) || st.st_size < 512) { + int ret = 1; + mode_t prev; + FILE *fp; +@@ -64,16 +65,34 @@ static void setup(void *arg) + prev = umask(077); + fp = fopen(RANDOMSEED, "w"); + if (fp) { +- int iter = 128; +- struct timeval tv; +- +- gettimeofday(&tv, NULL); +- srandom(tv.tv_sec % 3600); +- while (iter--) { +- uint32_t i, prng = random(); +- +- for (i = 0; i < sizeof(prng); i++) +- fputc((prng >> (i * CHAR_BIT)) & UCHAR_MAX, fp); ++ const char *hwrng = "/dev/hwrng"; ++ FILE *hw; ++ ++ hw = fopen(hwrng, "r"); ++ if (hw) { ++ char buf[512]; ++ size_t len; ++ ++ len = fread(buf, sizeof(buf[0]), sizeof(buf), hw); ++ if (len == 0) { ++ fclose(hw); ++ goto no_hwrng; ++ } ++ ++ len = fwrite(buf, sizeof(buf[0]), len, fp); ++ fclose(hw); ++ } else { ++ struct timeval tv; ++ int iter = 128; ++no_hwrng: ++ gettimeofday(&tv, NULL); ++ srandom(tv.tv_sec % 3600); ++ while (iter--) { ++ uint32_t i, prng = random(); ++ ++ for (i = 0; i < sizeof(prng); i++) ++ fputc((prng >> (i * CHAR_BIT)) & UCHAR_MAX, fp); ++ } + } + ret = fclose(fp); + } +-- +2.34.1 +