src_account_for_openssl_unexpected_version.patch 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Shelley Vohr <[email protected]>
  3. Date: Thu, 25 Jul 2024 12:19:41 +0200
  4. Subject: src: account for OpenSSL unexpected version
  5. Fixes a crash that occurs because the logic to parse for an OpenSSL
  6. version didn't account for OpenSSL_version returning a value that
  7. doesn't match the expected pattern of OpenSSL 1.1.0i 14 Aug 2018.
  8. In Electron's case, OpenSSL_version returns just BoringSSL, which in
  9. combination with the search logic not accounting for the delimiter not
  10. being present caused an out-of-bounds crash:
  11. out_of_range was thrown in -fno-exceptions mode with message "basic_string"
  12. This fixes that by checking for the null terminator and returning 0.0.0
  13. when the target delimiter isn't present.
  14. Upstreamed at https://github.com/nodejs/node/pull/54038
  15. diff --git a/src/node_metadata.cc b/src/node_metadata.cc
  16. index 5856292b5450f697cdb57de30bafd3e907a7964d..6cb32b187151b0416fbe29cf4b61543e828cea20 100644
  17. --- a/src/node_metadata.cc
  18. +++ b/src/node_metadata.cc
  19. @@ -48,14 +48,19 @@ Metadata metadata;
  20. #if HAVE_OPENSSL
  21. static constexpr size_t search(const char* s, char c, size_t n = 0) {
  22. - return *s == c ? n : search(s + 1, c, n + 1);
  23. + return *s == '\0' ? n : (*s == c ? n : search(s + 1, c, n + 1));
  24. }
  25. static inline std::string GetOpenSSLVersion() {
  26. // sample openssl version string format
  27. // for reference: "OpenSSL 1.1.0i 14 Aug 2018"
  28. const char* version = OpenSSL_version(OPENSSL_VERSION);
  29. - const size_t start = search(version, ' ') + 1;
  30. + const size_t first_space = search(version, ' ');
  31. + if (version[first_space] == '\0') {
  32. + return std::string("0.0.0");
  33. + }
  34. +
  35. + const size_t start = first_space + 1;
  36. const size_t len = search(&version[start], ' ');
  37. return std::string(version, start, len);
  38. }