From 345ca03d82d99c70495831b99654f6c3d333096f Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Fri, 10 Dec 2021 13:19:44 +0100 Subject: [PATCH 1/4] fix: Read Windows Version from Registry We were previously relying on the `kernel32.dll` version, as does Crashpad. However that version can lag behind OS releases, which can manifest itself as failing integration tests, as the version does not match the one returned from python `platform.version()`. --- src/sentry_os.c | 52 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/src/sentry_os.c b/src/sentry_os.c index 977261eba..47dd40fcc 100644 --- a/src/sentry_os.c +++ b/src/sentry_os.c @@ -1,9 +1,11 @@ #include "sentry_os.h" #include "sentry_string.h" +#include "sentry_utils.h" #ifdef SENTRY_PLATFORM_WINDOWS # include +# define CURRENT_VERSION "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" sentry_value_t sentry__get_os_context(void) @@ -34,17 +36,55 @@ sentry__get_os_context(void) } ffi->dwFileFlags &= ffi->dwFileFlagsMask; + uint32_t major_version = ffi->dwFileVersionMS >> 16; + uint32_t minor_version = ffi->dwFileVersionMS & 0xffff; + uint32_t build_version = ffi->dwFileVersionLS >> 16; + uint32_t ubr = ffi->dwFileVersionLS & 0xffff; + char buf[32]; - snprintf(buf, sizeof(buf), "%u.%u.%u", ffi->dwFileVersionMS >> 16, - ffi->dwFileVersionMS & 0xffff, ffi->dwFileVersionLS >> 16); + snprintf(buf, sizeof(buf), "%u.%u.%u.%lu", major_version, minor_version, + build_version, ubr); + sentry_value_set_by_key(os, "kernel_version", sentry_value_new_string(buf)); - sentry_value_set_by_key(os, "version", sentry_value_new_string(buf)); + sentry_free(ffibuf); - snprintf(buf, sizeof(buf), "%lu", ffi->dwFileVersionLS & 0xffff); + // The `CurrentMajorVersionNumber`, `CurrentMinorVersionNumber` and `UBR` + // are DWORD, while `CurrentBuild` is a SZ (text). - sentry_value_set_by_key(os, "build", sentry_value_new_string(buf)); + uint32_t reg_version = 0; + DWORD buf_size = sizeof(uint32_t); + if (RegGetValueA(HKEY_LOCAL_MACHINE, CURRENT_VERSION, + "CurrentMajorVersionNumber", RRF_RT_REG_DWORD, NULL, ®_version, + &buf_size) + == ERROR_SUCCESS) { + major_version = reg_version; + } + buf_size = sizeof(uint32_t); + if (RegGetValueA(HKEY_LOCAL_MACHINE, CURRENT_VERSION, + "CurrentMinorVersionNumber", RRF_RT_REG_DWORD, NULL, ®_version, + &buf_size) + == ERROR_SUCCESS) { + minor_version = reg_version; + } + buf_size = sizeof(buf); + if (RegGetValueA(HKEY_LOCAL_MACHINE, CURRENT_VERSION, "CurrentBuild", + RRF_RT_REG_SZ, NULL, buf, &buf_size) + == ERROR_SUCCESS) { + build_version = (uint32_t)sentry__strtod_c(buf, NULL); + } + buf_size = sizeof(uint32_t); + if (RegGetValueA(HKEY_LOCAL_MACHINE, CURRENT_VERSION, "UBR", + RRF_RT_REG_DWORD, NULL, ®_version, &buf_size) + == ERROR_SUCCESS) { + ubr = reg_version; + } - sentry_free(ffibuf); + snprintf(buf, sizeof(buf), "%u.%u.%u", major_version, minor_version, + build_version); + sentry_value_set_by_key(os, "version", sentry_value_new_string(buf)); + + snprintf(buf, sizeof(buf), "%lu", ubr); + sentry_value_set_by_key(os, "build", sentry_value_new_string(buf)); sentry_value_freeze(os); return os; From 42ccd1cb956b8803c8590125fafc8bf78254a3af Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Tue, 18 Jan 2022 13:24:20 +0100 Subject: [PATCH 2/4] use ntoskrnl.exe as kernel version --- src/sentry_os.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sentry_os.c b/src/sentry_os.c index 47dd40fcc..beeddaece 100644 --- a/src/sentry_os.c +++ b/src/sentry_os.c @@ -19,13 +19,13 @@ sentry__get_os_context(void) void *ffibuf = NULL; - DWORD size = GetFileVersionInfoSizeW(L"kernel32.dll", NULL); + DWORD size = GetFileVersionInfoSizeW(L"ntoskrnl.exe", NULL); if (!size) { goto fail; } ffibuf = sentry_malloc(size); - if (!GetFileVersionInfoW(L"kernel32.dll", 0, size, ffibuf)) { + if (!GetFileVersionInfoW(L"ntoskrnl.exe", 0, size, ffibuf)) { goto fail; } From de278844a9eb5a934359dec1a5d11d80a78124f3 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Tue, 18 Jan 2022 13:52:01 +0100 Subject: [PATCH 3/4] fall back to kernel32.dll --- src/sentry_os.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/sentry_os.c b/src/sentry_os.c index beeddaece..843e24756 100644 --- a/src/sentry_os.c +++ b/src/sentry_os.c @@ -7,6 +7,23 @@ # include # define CURRENT_VERSION "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" +void * +sentry__try_file_version(LPCWSTR filename) +{ + + DWORD size = GetFileVersionInfoSizeW(L"ntoskrnl.exe", NULL); + if (!size) { + return NULL; + } + + void *ffibuf = sentry_malloc(size); + if (!GetFileVersionInfoW(L"ntoskrnl.exe", 0, size, ffibuf)) { + sentry_free(ffibuf); + return NULL; + } + return ffibuf; +} + sentry_value_t sentry__get_os_context(void) { @@ -17,15 +34,11 @@ sentry__get_os_context(void) sentry_value_set_by_key(os, "name", sentry_value_new_string("Windows")); - void *ffibuf = NULL; - - DWORD size = GetFileVersionInfoSizeW(L"ntoskrnl.exe", NULL); - if (!size) { - goto fail; + void *ffibuf = sentry__try_file_version(L"ntoskrnl.exe"); + if (!ffibuf) { + ffibuf = sentry__try_file_version(L"kernel32.dll"); } - - ffibuf = sentry_malloc(size); - if (!GetFileVersionInfoW(L"ntoskrnl.exe", 0, size, ffibuf)) { + if (!ffibuf) { goto fail; } From 7ffc5b30dbcc17e3a090e1c2157bb6faccba12f8 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Tue, 18 Jan 2022 14:39:39 +0100 Subject: [PATCH 4/4] lol, im an idiot --- src/sentry_os.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sentry_os.c b/src/sentry_os.c index 843e24756..fde706684 100644 --- a/src/sentry_os.c +++ b/src/sentry_os.c @@ -11,13 +11,13 @@ void * sentry__try_file_version(LPCWSTR filename) { - DWORD size = GetFileVersionInfoSizeW(L"ntoskrnl.exe", NULL); + DWORD size = GetFileVersionInfoSizeW(filename, NULL); if (!size) { return NULL; } void *ffibuf = sentry_malloc(size); - if (!GetFileVersionInfoW(L"ntoskrnl.exe", 0, size, ffibuf)) { + if (!GetFileVersionInfoW(filename, 0, size, ffibuf)) { sentry_free(ffibuf); return NULL; }