0001-ustream-mbedtls-use-getrandom-instead-of-dev-urandom.patch 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. From 498f6e268d4d2b0ad33b430f4ba1abe397d31496 Mon Sep 17 00:00:00 2001
  2. From: Hauke Mehrtens <hauke@hauke-m.de>
  3. Date: Sun, 19 Feb 2023 21:11:12 +0100
  4. Subject: [PATCH] ustream-mbedtls: Use getrandom() instead of /dev/urandom
  5. Instead of keeping a file descriptor open just use the getrandom syscall
  6. to get random data. This is supported by musl libc, glibc and Linux for
  7. some time now.
  8. This also improves the error handling in case this function returns not
  9. as many bytes as expected.
  10. Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
  11. Reviewed-by: Torsten Duwe <duwe@lst.de>
  12. Upstream: https://git.openwrt.org/?p=project/ustream-ssl.git;a=commit;h=498f6e268d4d2b0ad33b430f4ba1abe397d31496
  13. Signed-off-by: Thomas Perale <thomas.perale@mind.be>
  14. ---
  15. ustream-mbedtls.c | 25 ++++++-------------------
  16. 1 file changed, 6 insertions(+), 19 deletions(-)
  17. diff --git a/ustream-mbedtls.c b/ustream-mbedtls.c
  18. index e79e37b..7fc7874 100644
  19. --- a/ustream-mbedtls.c
  20. +++ b/ustream-mbedtls.c
  21. @@ -17,6 +17,7 @@
  22. */
  23. #include <sys/types.h>
  24. +#include <sys/random.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. @@ -25,8 +26,6 @@
  29. #include "ustream-ssl.h"
  30. #include "ustream-internal.h"
  31. -static int urandom_fd = -1;
  32. -
  33. static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
  34. {
  35. struct ustream *s = ctx;
  36. @@ -66,21 +65,12 @@ __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustr
  37. mbedtls_ssl_set_bio(ssl, conn, s_ustream_write, s_ustream_read, NULL);
  38. }
  39. -static bool urandom_init(void)
  40. +static int _random(void *ctx, unsigned char *out, size_t len)
  41. {
  42. - if (urandom_fd > -1)
  43. - return true;
  44. + ssize_t ret;
  45. - urandom_fd = open("/dev/urandom", O_RDONLY);
  46. - if (urandom_fd < 0)
  47. - return false;
  48. -
  49. - return true;
  50. -}
  51. -
  52. -static int _urandom(void *ctx, unsigned char *out, size_t len)
  53. -{
  54. - if (read(urandom_fd, out, len) < 0)
  55. + ret = getrandom(out, len, 0);
  56. + if (ret < 0 || (size_t)ret != len)
  57. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  58. return 0;
  59. @@ -134,9 +124,6 @@ __ustream_ssl_context_new(bool server)
  60. mbedtls_ssl_config *conf;
  61. int ep;
  62. - if (!urandom_init())
  63. - return NULL;
  64. -
  65. ctx = calloc(1, sizeof(*ctx));
  66. if (!ctx)
  67. return NULL;
  68. @@ -159,7 +146,7 @@ __ustream_ssl_context_new(bool server)
  69. mbedtls_ssl_config_defaults(conf, ep, MBEDTLS_SSL_TRANSPORT_STREAM,
  70. MBEDTLS_SSL_PRESET_DEFAULT);
  71. - mbedtls_ssl_conf_rng(conf, _urandom, NULL);
  72. + mbedtls_ssl_conf_rng(conf, _random, NULL);
  73. if (server) {
  74. mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
  75. --
  76. 2.30.2