Skip to contentSkip to conversation

qga: Move CoInitialize/CoInitializeSecurity to main process thread

PATCH In review Elizabeth Ashurov 4 participants 6 messages Feb 11 – Oct 14

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 qga: Move CoInitialize/CoInitializeSecurity to main process thread

Problem:
Two issues with COM initialization:

Issue #1: Incorrect call order
- requester_init() called CoInitializeSecurity first
- Per Microsoft documentation, CoInitialize() must be called BEFORE CoInitializeSecurity()

Issue #2: Incorrect call location
- CoInitializeSecurity was called from dll instead of the main process
- Per Microsoft documentation, CoInitializeSecurity() must be called exactly once per process from the main executable, not from a DLL

Reference:
https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializesecurity
https://learn.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize

This caused incorrect COM initialization, preventing VSS Writers from calling back via IVssWriterCallback (hr = 0x80070005, Access denied, Event ID 8194).

Fix:
- Initialize COM in main.c for both service and CLI modes
- Call CoInitialize() followed by CoInitializeSecurity() in correct order in the main thread before any VSS operations
- Add proper CoUninitialize() cleanup

Result:
VSS Writers can now successfully call back to the agent. Event ID 8194 error is resolved.

---
 qga/main.c                  | 42 +++++++++++++++++++++++++++++++++++++
 qga/vss-win32/requester.cpp | 13 ------------
 2 files changed, 42 insertions(+), 13 deletions(-)
qga/main.c +42 -0
index dd1c216f9a..fd19c7037d 100644
@@ -32,6 +32,8 @@
 #include "qemu/systemd.h"
 #include "qemu-version.h"
 #ifdef _WIN32
+#include <windows.h>
+#include <objbase.h>
 #include <dbt.h>
 #include <pdh.h>
 #include "qga/service-win32.h"
@@ -830,6 +832,29 @@ DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
     return ret;
 }
 
+/* Initialize COM for VSS operations */
+static HRESULT init_com(void)
+{
+    HRESULT hr;
+
+    hr = CoInitialize(NULL);
+    if (FAILED(hr)) {
+        return hr;
+    }
+
+    hr = CoInitializeSecurity(
+        NULL, -1, NULL, NULL,
+        RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
+        RPC_C_IMP_LEVEL_IDENTIFY,
+        NULL, EOAC_NONE, NULL);
+    if (FAILED(hr)) {
+        CoUninitialize();
+        return hr;
+    }
+
+    return S_OK;
+}
+
 VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
 {
     GAService *service = &ga_state->service;
@@ -842,6 +867,13 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
         return;
     }
 
+    /* Initialize COM for VSS operations in the service thread */
+    HRESULT hr_com = init_com();
+    if (FAILED(hr_com)) {
+        g_critical("Failed to initialize COM in service thread: 0x%lx", hr_com);
+        return;
+    }
+
     service->status.dwServiceType = SERVICE_WIN32;
     service->status.dwCurrentState = SERVICE_RUNNING;
     service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
@@ -866,6 +898,8 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
 
     run_agent(ga_state);
 
+    CoUninitialize();
+
     UnregisterDeviceNotification(service->device_notification_handle);
     service->status.dwCurrentState = SERVICE_STOPPED;
     SetServiceStatus(service->status_handle, &service->status);
@@ -1719,7 +1753,15 @@ int main(int argc, char **argv)
         StartServiceCtrlDispatcher(service_table);
         ret = EXIT_SUCCESS;
     } else {
+        HRESULT hr_com = init_com();
+        if (FAILED(hr_com)) {
+            g_critical("Failed to initialize COM: 0x%lx", hr_com);
+            ret = EXIT_FAILURE;
+            goto end;
+        }
+
         ret = run_agent(s);
+        CoUninitialize();
     }
 #else
     ret = run_agent(s);
qga/vss-win32/requester.cpp +0 -13
index 74489fcd0a..626d6ab1ff 100644
@@ -60,15 +60,6 @@ STDAPI requester_init(void)
 {
     qga_debug_begin;
 
-    COMInitializer initializer; /* to call CoInitializeSecurity */
-    HRESULT hr = CoInitializeSecurity(
-        NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
-        RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
-    if (FAILED(hr)) {
-        qga_debug("failed to CoInitializeSecurity (error %lx)", hr);
-        return hr;
-    }
-
     hLib = LoadLibraryA("VSSAPI.DLL");
     if (!hLib) {
         qga_debug("failed to load VSSAPI.DLL");
@@ -320,8 +311,6 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset)
         return;
     }
 
-    CoInitialize(NULL);
-
     /* Allow unrestricted access to events */
     InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
     SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
@@ -562,7 +551,6 @@ out:
 
 out1:
     requester_cleanup();
-    CoUninitialize();
 
     qga_debug_end;
 }
@@ -643,7 +631,6 @@ void requester_thaw(int *num_vols, void *mountpints, ErrorSet *errset)
     *num_vols = vss_ctx.cFrozenVols;
     requester_cleanup();
 
-    CoUninitialize();
     StopService();
 
     qga_debug_end;
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 2/2 qga: Fix VSS provider GetSnapshotProperties return value

GetSnapshotProperties() returned VSS_E_OBJECT_NOT_FOUND causing "Snapshot id not found" errors during VSS operations.

Any error in this function causes a similar error in Windows VSS, so we must return S_OK there.

---
 qga/vss-win32/provider.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qga/vss-win32/provider.cpp +1 -1
index a102a23fbf..2b5c6f8e8e 100644
@@ -263,7 +263,7 @@ STDMETHODIMP CQGAVssProvider::SetContext(LONG lContext)
 STDMETHODIMP CQGAVssProvider::GetSnapshotProperties(
     VSS_ID SnapshotId, VSS_SNAPSHOT_PROP *pProp)
 {
-    return VSS_E_OBJECT_NOT_FOUND;
+    return S_OK;
 }
 
 STDMETHODIMP CQGAVssProvider::Query(
Signature · 1 line
2.51.0
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] qga: Fix VSS provider GetSnapshotProperties return value

Quoted 28 lines · Elizabeth Ashurov

GetSnapshotProperties() returned VSS_E_OBJECT_NOT_FOUND causing "Snapshot id not found" errors during VSS operations.

Any error in this function causes a similar error in Windows VSS, so we must return S_OK there.

---
 qga/vss-win32/provider.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qga/vss-win32/provider.cpp +1 -2
index a102a23fbf..2b5c6f8e8e 100644
@@ -263,7 +263,7 @@ STDMETHODIMP CQGAVssProvider::SetContext(LONG lContext)
 STDMETHODIMP CQGAVssProvider::GetSnapshotProperties(
     VSS_ID SnapshotId, VSS_SNAPSHOT_PROP *pProp)
 {
-    return VSS_E_OBJECT_NOT_FOUND;
+    return S_OK;
 }
 
 STDMETHODIMP CQGAVssProvider::Query(
--

2.51.0

#3 lore raw

Re: [PATCH v2 2/2] qga: Fix VSS provider GetSnapshotProperties return value

In reply to Elizabeth Ashurov
Quoted 8 lines · Elizabeth Ashurov

GetSnapshotProperties() returned VSS_E_OBJECT_NOT_FOUND causing "Snapshot id not found" errors during VSS operations.

Any error in this function causes a similar error in Windows VSS, so we must return S_OK there.

Hi Elizabeth, Kostiantyn,

Commit 374f4676788c ("qga: Fix VSS provider GetSnapshotProperties return value"), also shipped in qemu-ga-win 110.2.1+ / virtio-win 0.1.302, breaks third-party VSS requesters that never use the QGA provider.

CQGAVssProvider::GetSnapshotProperties() now returns S_OK for any snapshot id without filling pProp. A requester that creates a snapshot set with AddToSnapshotSet(vol, GUID_NULL, ...) gets a valid shadow copy from the Microsoft Software Shadow Copy provider, but IVssBackupComponents::GetSnapshotProperties() on that snapshot then returns S_OK with an all-zero VSS_SNAPSHOT_PROP (provider id, snapshot set id, original volume and device object all NULL). IVssBackupComponents::Query() on the same snapshot still returns the correct properties, which is why diskshadow is not affected.

This was found in production: a backup agent on Windows Server 2019/2022 guests (Proxmox VE) stopped working right after the virtio-win 0.1.302 upgrade. Downgrading qemu-ga-win to 110.0.2 fixes it.

Reproduced on a plain Windows 11 23H2 machine (no VM needed) with a minimal requester, in both VSS_CTX_FILE_SHARE_BACKUP and VSS_CTX_BACKUP (writers) contexts:

  qemu-ga-win     GetSnapshotProperties()          Query()
  not installed   Microsoft provider, device set   same
  110.2.3         S_OK, all fields zero            Microsoft provider, device set
  110.0.2         Microsoft provider, device set   same

Note for anyone reproducing: after (un)installing the MSI, the "QEMU Guest Agent VSS Provider" service keeps the previously loaded qga-vss.dll until it is restarted (net stop "QEMU Guest Agent VSS Provider" && net stop vss, or reboot).

Details, output and the reproducer source are in
https://gitlab.com/qemu-project/qemu/-/work_items/4675

The IVssSoftwareSnapshotProvider::GetSnapshotProperties documentation expects the provider to fill the structure when it returns S_OK. Would it be possible to return VSS_E_OBJECT_NOT_FOUND for snapshot ids the QGA provider does not own, and S_OK (with populated properties) only for its own snapshots? The "Snapshot id not found" error that 374f4676788c addressed could then be handled in that narrower scope.

I can test a candidate fix with the same reproducer.

The analysis was done with the help of an LLM-based assistant; the reproduction was run and checked by me.

Thanks,
Silvain Combis-Schlumberger

Reviewed-by on v2 1/2
#4 lore raw
In reply to Elizabeth Ashurov
Quoted 176 lines · Elizabeth Ashurov

Problem:
Two issues with COM initialization:

Issue #1: Incorrect call order
- requester_init() called CoInitializeSecurity first
- Per Microsoft documentation, CoInitialize() must be called BEFORE CoInitializeSecurity()

Issue #2: Incorrect call location
- CoInitializeSecurity was called from dll instead of the main process
- Per Microsoft documentation, CoInitializeSecurity() must be called exactly once per process from
the main executable, not from a DLL

Reference:

https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializesecurity

https://learn.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize

This caused incorrect COM initialization, preventing VSS Writers from calling back via IVssWriterCallback (hr = 0x80070005, Access denied, Event ID 8194).

Fix:
- Initialize COM in main.c for both service and CLI modes
- Call CoInitialize() followed by CoInitializeSecurity() in correct order in the main thread before any VSS operations
- Add proper CoUninitialize() cleanup

Result:
VSS Writers can now successfully call back to the agent. Event ID 8194 error is resolved.

---
 qga/main.c                  | 42 +++++++++++++++++++++++++++++++++++++
 qga/vss-win32/requester.cpp | 13 ------------
 2 files changed, 42 insertions(+), 13 deletions(-)
qga/main.c +25 -0
index dd1c216f9a..fd19c7037d 100644
@@ -32,6 +32,8 @@
 #include "qemu/systemd.h"
 #include "qemu-version.h"
 #ifdef _WIN32
+#include <windows.h>
+#include <objbase.h>
 #include <dbt.h>
 #include <pdh.h>
 #include "qga/service-win32.h"
@@ -830,6 +832,29 @@ DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD
 type, LPVOID data,
     return ret;
 }
 
+/* Initialize COM for VSS operations */
+static HRESULT init_com(void)
+{
+    HRESULT hr;
+
+    hr = CoInitialize(NULL);
+    if (FAILED(hr)) {
+        return hr;
+    }
+
+    hr = CoInitializeSecurity(
+        NULL, -1, NULL, NULL,
+        RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
+        RPC_C_IMP_LEVEL_IDENTIFY,
+        NULL, EOAC_NONE, NULL);
+    if (FAILED(hr)) {
+        CoUninitialize();
+        return hr;
+    }
+
+    return S_OK;
+}
+
 VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
 {
     GAService *service = &ga_state->service;
qga/main.c +7 -0
@@ -842,6 +867,13 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
         return;
     }
 
+    /* Initialize COM for VSS operations in the service thread */
+    HRESULT hr_com = init_com();
+    if (FAILED(hr_com)) {
+        g_critical("Failed to initialize COM in service thread: 0x%lx",
 hr_com);
+        return;
+    }
+
     service->status.dwServiceType = SERVICE_WIN32;
     service->status.dwCurrentState = SERVICE_RUNNING;
     service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP |

SERVICE_ACCEPT_SHUTDOWN;

qga/main.c +10 -0
@@ -866,6 +898,8 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
 
     run_agent(ga_state);
 
+    CoUninitialize();
+
     UnregisterDeviceNotification(service->device_notification_handle);
     service->status.dwCurrentState = SERVICE_STOPPED;
     SetServiceStatus(service->status_handle, &service->status);
@@ -1719,7 +1753,15 @@ int main(int argc, char **argv)
         StartServiceCtrlDispatcher(service_table);
         ret = EXIT_SUCCESS;
     } else {
+        HRESULT hr_com = init_com();
+        if (FAILED(hr_com)) {
+            g_critical("Failed to initialize COM: 0x%lx", hr_com);
+            ret = EXIT_FAILURE;
+            goto end;
+        }
+
         ret = run_agent(s);
+        CoUninitialize();
     }
 #else
     ret = run_agent(s);
qga/vss-win32/requester.cpp +0 -11
index 74489fcd0a..626d6ab1ff 100644
@@ -60,15 +60,6 @@ STDAPI requester_init(void)
 {
     qga_debug_begin;
 
-    COMInitializer initializer; /* to call CoInitializeSecurity */
-    HRESULT hr = CoInitializeSecurity(
-        NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
-        RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
-    if (FAILED(hr)) {
-        qga_debug("failed to CoInitializeSecurity (error %lx)", hr);
-        return hr;
-    }
-
     hLib = LoadLibraryA("VSSAPI.DLL");
     if (!hLib) {
         qga_debug("failed to load VSSAPI.DLL");
@@ -320,8 +311,6 @@ void requester_freeze(int *num_vols, void
 *mountpoints, ErrorSet *errset)
         return;
     }
 
-    CoInitialize(NULL);
-
     /* Allow unrestricted access to events */
     InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
     SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
qga/vss-win32/requester.cpp +0 -2
@@ -562,7 +551,6 @@ out:
 
 out1:
     requester_cleanup();
-    CoUninitialize();
 
     qga_debug_end;
 }
@@ -643,7 +631,6 @@ void requester_thaw(int *num_vols, void *mountpints,
 ErrorSet *errset)
     *num_vols = vss_ctx.cFrozenVols;
     requester_cleanup();
 
-    CoUninitialize();
     StopService();
 
     qga_debug_end;

--
2.51.0

Tested-by
#5 lore raw
Quoted 181 lines · Kostiantyn Kostiuk
Quoted 176 lines · Elizabeth Ashurov

Problem:
Two issues with COM initialization:

Issue #1: Incorrect call order
- requester_init() called CoInitializeSecurity first
- Per Microsoft documentation, CoInitialize() must be called BEFORE CoInitializeSecurity()

Issue #2: Incorrect call location
- CoInitializeSecurity was called from dll instead of the main process
- Per Microsoft documentation, CoInitializeSecurity() must be called exactly once per process from
the main executable, not from a DLL

Reference:

https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializesecurity

https://learn.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize

This caused incorrect COM initialization, preventing VSS Writers from calling back via IVssWriterCallback (hr = 0x80070005, Access denied, Event ID 8194).

Fix:
- Initialize COM in main.c for both service and CLI modes
- Call CoInitialize() followed by CoInitializeSecurity() in correct order in the main thread before any VSS operations
- Add proper CoUninitialize() cleanup

Result:
VSS Writers can now successfully call back to the agent. Event ID 8194 error is resolved.

---
 qga/main.c                  | 42 +++++++++++++++++++++++++++++++++++++
 qga/vss-win32/requester.cpp | 13 ------------
 2 files changed, 42 insertions(+), 13 deletions(-)
qga/main.c +25 -0
index dd1c216f9a..fd19c7037d 100644
@@ -32,6 +32,8 @@
 #include "qemu/systemd.h"
 #include "qemu-version.h"
 #ifdef _WIN32
+#include <windows.h>
+#include <objbase.h>
 #include <dbt.h>
 #include <pdh.h>
 #include "qga/service-win32.h"
@@ -830,6 +832,29 @@ DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD
 type, LPVOID data,
     return ret;
 }
 
+/* Initialize COM for VSS operations */
+static HRESULT init_com(void)
+{
+    HRESULT hr;
+
+    hr = CoInitialize(NULL);
+    if (FAILED(hr)) {
+        return hr;
+    }
+
+    hr = CoInitializeSecurity(
+        NULL, -1, NULL, NULL,
+        RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
+        RPC_C_IMP_LEVEL_IDENTIFY,
+        NULL, EOAC_NONE, NULL);
+    if (FAILED(hr)) {
+        CoUninitialize();
+        return hr;
+    }
+
+    return S_OK;
+}
+
 VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
 {
     GAService *service = &ga_state->service;
qga/main.c +7 -0
@@ -842,6 +867,13 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
         return;
     }
 
+    /* Initialize COM for VSS operations in the service thread */
+    HRESULT hr_com = init_com();
+    if (FAILED(hr_com)) {
+        g_critical("Failed to initialize COM in service thread: 0x%lx",
 hr_com);
+        return;
+    }
+
     service->status.dwServiceType = SERVICE_WIN32;
     service->status.dwCurrentState = SERVICE_RUNNING;
     service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP |

SERVICE_ACCEPT_SHUTDOWN;

qga/main.c +10 -0
@@ -866,6 +898,8 @@ VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
 
     run_agent(ga_state);
 
+    CoUninitialize();
+
     UnregisterDeviceNotification(service->device_notification_handle);
     service->status.dwCurrentState = SERVICE_STOPPED;
     SetServiceStatus(service->status_handle, &service->status);
@@ -1719,7 +1753,15 @@ int main(int argc, char **argv)
         StartServiceCtrlDispatcher(service_table);
         ret = EXIT_SUCCESS;
     } else {
+        HRESULT hr_com = init_com();
+        if (FAILED(hr_com)) {
+            g_critical("Failed to initialize COM: 0x%lx", hr_com);
+            ret = EXIT_FAILURE;
+            goto end;
+        }
+
         ret = run_agent(s);
+        CoUninitialize();
     }
 #else
     ret = run_agent(s);
qga/vss-win32/requester.cpp +0 -11
index 74489fcd0a..626d6ab1ff 100644
@@ -60,15 +60,6 @@ STDAPI requester_init(void)
 {
     qga_debug_begin;
 
-    COMInitializer initializer; /* to call CoInitializeSecurity */
-    HRESULT hr = CoInitializeSecurity(
-        NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
-        RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
-    if (FAILED(hr)) {
-        qga_debug("failed to CoInitializeSecurity (error %lx)", hr);
-        return hr;
-    }
-
     hLib = LoadLibraryA("VSSAPI.DLL");
     if (!hLib) {
         qga_debug("failed to load VSSAPI.DLL");
@@ -320,8 +311,6 @@ void requester_freeze(int *num_vols, void
 *mountpoints, ErrorSet *errset)
         return;
     }
 
-    CoInitialize(NULL);
-
     /* Allow unrestricted access to events */
     InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
     SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
qga/vss-win32/requester.cpp +0 -2
@@ -562,7 +551,6 @@ out:
 
 out1:
     requester_cleanup();
-    CoUninitialize();
 
     qga_debug_end;
 }
@@ -643,7 +631,6 @@ void requester_thaw(int *num_vols, void *mountpints,
 ErrorSet *errset)
     *num_vols = vss_ctx.cFrozenVols;
     requester_cleanup();
 
-    CoUninitialize();
     StopService();
 
     qga_debug_end;

--
2.51.0