Various QGA VSS fixes
cover Various QGA VSS fixes
Hi,
While studying QGA VSS code, I collected some correctness and robustness patches.
---
Marc-André Lureau (17):
qga/installer: preserve VssOption registry value across upgrades qga/vss-win32: fix race condition in COM Release() methods qga/vss-win32: pass valid SERVICE_STATUS to ControlService qga/vss-win32: explicit NULL/zero initialization qga/vss-win32: free bstrWriterName in outer writer loop qga/vss-win32: pass character count instead of byte count to volume APIs qga/vss-win32: allow pceltFetched==NULL in IEnumObject::Next qga/vss-win32: return an interface pointer for IID_IUnknown qga/vss-win32: use GetLastError() after FindFirstVolumeW failure qga/vss-win32: guard CloseServiceHandle calls against NULL handles qga/vss-win32: avoid CRT calls in DllMain debug logging qga/vss-win32: evaluate err_set() macro argument only once qga/vss-win32: report QueryStatus return value on failure qga/vss-win32: check GetLastError() after FindNextVolumeW failure qga/vss-win32: initialize *num_vols at start of requester_freeze qga/vss-win32: restrict access to freeze/thaw synchronisation events qga/vss-win32: log to file during COM+ registration
qga/installer/qemu-ga.wxs | 19 ++++++++-- qga/vss-win32/install.cpp | 31 ++++++++++++++--- qga/vss-win32/provider.cpp | 17 +++++---- qga/vss-win32/requester.cpp | 85 +++++++++++++++++++++++++++++++++++++-------- 4 files changed, 124 insertions(+), 28 deletions(-)
---
base-commit: ff1d2d19d7e24893e2012d879f8e73077e17b9bd
change-id: 20260902-qga-6b06950cd896
Best regards,
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH 1/17 qga/installer: preserve VssOption registry value across upgrades
The VssOption registry value was unconditionally written to 1 on every install, overwriting any administrator customization. Additionally, it was created even when VSS support was not being installed.
Move VssOption into its own component guarded by the InstallVss preprocessor conditional. Use a RegistrySearch to read any existing value into the VSSOPTION_VALUE property (defaulting to #1 for fresh installs), and write it back via MSI property substitution so admin customizations survive upgrades.
Update the registry_entries component GUID since its resource set changed.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2919
--- qga/installer/qemu-ga.wxs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
qga/installer/qemu-ga.wxs +17 -2
@@ -31,6 +31,15 @@ /> <Media Id="1" Cabinet="qemu_ga.$(var.QEMU_GA_VERSION).cab" EmbedCab="yes" /> <Property Id="WHSLogo">1</Property> + <?ifdef var.InstallVss?> + <Property Id="VSSOPTION_VALUE" Value="#1"> + <RegistrySearch Id="VssOptionSearch" + Root="HKLM" + Key="System\CurrentControlSet\Services\QEMU Guest Agent VSS Provider" + Name="VssOption" + Type="raw" /> + </Property> + <?endif?> <Property Id="ARPNOMODIFY" Value="yes" Secure="yes" /> <MajorUpgrade DowngradeErrorMessage="Error: A newer version of QEMU guest agent is already installed."
@@ -111,7 +120,7 @@ <File Id="libpcre2_8_0.dll" Name="libpcre2-8-0.dll" Source="$(var.BIN_DIR)/libpcre2-8-0.dll" KeyPath="yes" DiskId="1"/> </Component> <?endif?> - <Component Id="registry_entries" Guid="{D075D109-51CA-11E3-9F8B-000C29858960}"> + <Component Id="registry_entries" Guid="{62A2D9E2-B22F-410C-8E41-A0D9E653B944}"> <RegistryKey Root="HKLM" Key="Software\$(var.QEMU_GA_MANUFACTURER)\$(var.QEMU_GA_DISTRO)\Tools\QemuGA"> <RegistryValue Type="string" Name="ProductID" Value="fb0a0d66-c7fb-4e2e-a16b-c4a3bfe8d13b" />
@@ -122,11 +131,16 @@ <RegistryValue Type="integer" Name="TypesSupported" Value="7" /> <RegistryValue Type="string" Name="EventMessageFile" Value="[qemu_ga_directory]qemu-ga.exe" /> </RegistryKey> + </Component> + <?ifdef var.InstallVss?> + <Component Id="vss_registry_option" Guid="{75166af0-cdc9-423f-9a30-50bb59342b9b}"> <RegistryKey Root="HKLM" Key="System\CurrentControlSet\Services\QEMU Guest Agent VSS Provider"> - <RegistryValue Type="integer" Name="VssOption" Value="1" /> + <!-- Type="string" so MSI resolves [VSSOPTION_VALUE]; the "#N" raw format produces a REG_DWORD. --> + <RegistryValue Type="string" Name="VssOption" Value="[VSSOPTION_VALUE]" KeyPath="yes" /> </RegistryKey> </Component> + <?endif?> </Directory> </Directory> </Directory>
@@ -167,6 +181,7 @@ <ComponentRef Id="libstdc++_6_lib" /> <ComponentRef Id="qga_vss_dll" /> <ComponentRef Id="qga_vss_tlb" /> + <ComponentRef Id="vss_registry_option" /> <?endif?> <ComponentRef Id="gspawn-helper-console" /> <ComponentRef Id="gspawn-helper" />
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 01/17] qga/installer: preserve VssOption registry value across upgrades
Quoted 93 lines · Marc-André Lureau
The VssOption registry value was unconditionally written to 1 on every install, overwriting any administrator customization. Additionally, it was created even when VSS support was not being installed.
Move VssOption into its own component guarded by the InstallVss preprocessor conditional. Use a RegistrySearch to read any existing value into the
VSSOPTION_VALUEproperty (defaulting to #1 for fresh installs), and write it back via MSI property substitution so admin customizations survive upgrades.Update the
registry_entriescomponent GUID since its resource set changed.Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2919
Signed-off-by Marc-André Lureau <[email protected]>--- qga/installer/qemu-ga.wxs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)qga/installer/qemu-ga.wxs +0
-0@@ -31,6 +31,15 @@ /> <Media Id="1" Cabinet="qemu_ga.$(var.QEMU_GA_VERSION).cab"EmbedCab="yes" />
<Property Id="WHSLogo">1</Property>qga/installer/qemu-ga.wxs +5
-0+ <?ifdef var.InstallVss?> + <Property Id="VSSOPTION_VALUE" Value="#1"> + <RegistrySearch Id="VssOptionSearch" + Root="HKLM" + Key="System\CurrentControlSet\Services\QEMU GuestAgent VSS Provider"
qga/installer/qemu-ga.wxs +4
-0+ Name="VssOption" + Type="raw" /> + </Property> + <?endif?> <Property Id="ARPNOMODIFY" Value="yes" Secure="yes" /> <MajorUpgrade DowngradeErrorMessage="Error: A newer version of QEMU guest agentis already installed."
qga/installer/qemu-ga.wxs +0
-0@@ -111,7 +120,7 @@ <File Id="libpcre2_8_0.dll" Name="libpcre2-8-0.dll"Source="$(var.BIN_DIR)/libpcre2-8-0.dll" KeyPath="yes" DiskId="1"/>
</Component> <?endif?>- <Component Id="registry_entries"
Guid="{D075D109-51CA-11E3-9F8B-000C29858960}">
+ <Component Id="registry_entries"
Guid="{62A2D9E2-B22F-410C-8E41-A0D9E653B944}">
<RegistryKey Root="HKLM"Key="Software\$(var.QEMU_GA_MANUFACTURER)\$(var.QEMU_GA_DISTRO)\Tools\QemuGA">
<RegistryValue Type="string" Name="ProductID"
Value="fb0a0d66-c7fb-4e2e-a16b-c4a3bfe8d13b" />qga/installer/qemu-ga.wxs +0
-0@@ -122,11 +131,16 @@ <RegistryValue Type="integer" Name="TypesSupported"Value="7" />
<RegistryValue Type="string" Name="EventMessageFile"
Value="[qemu_ga_directory]qemu-ga.exe" />
</RegistryKey>qga/installer/qemu-ga.wxs +3
-0+ </Component> + <?ifdef var.InstallVss?> + <Component Id="vss_registry_option"Guid="{75166af0-cdc9-423f-9a30-50bb59342b9b}">
<RegistryKey Root="HKLM"
Key="System\CurrentControlSet\Services\QEMU
Guest Agent VSS Provider">qga/installer/qemu-ga.wxs +1
-1- <RegistryValue Type="integer" Name="VssOption" Value="1" /> + <!-- Type="string" so MSI resolves [VSSOPTION_VALUE]; the"#N" raw format produces a
REG_DWORD. -->
+ <RegistryValue Type="string" Name="VssOption"
Value="[VSSOPTION_VALUE]" KeyPath="yes" />
</RegistryKey>
</Component>qga/installer/qemu-ga.wxs +2
-1+ <?endif?> </Directory> </Directory> </Directory>@@ -167,6 +181,7 @@ <ComponentRef Id="libstdc++_6_lib" /> <ComponentRef Id="qga_vss_dll" /> <ComponentRef Id="qga_vss_tlb" /> + <ComponentRef Id="vss_registry_option" /> <?endif?> <ComponentRef Id="gspawn-helper-console" /> <ComponentRef Id="gspawn-helper" /> --2.55.0.543.g5ebe2ebe4ea8
PATCH 2/17 qga/vss-win32: fix race condition in COM Release() methods
In reply to Marc-André LureauAll three Release() implementations checked the member m_nRefCount after InterlockedDecrement instead of using the local return value. In a multi-threaded context (vssvc), two threads decrementing concurrently could both read m_nRefCount as 0 and double-free.
--- qga/vss-win32/provider.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
qga/vss-win32/provider.cpp +3 -3
@@ -108,7 +108,7 @@ STDMETHODIMP_(ULONG) CQGAVSSEnumObject::AddRef() STDMETHODIMP_(ULONG) CQGAVSSEnumObject::Release() { long nRefCount = InterlockedDecrement(&m_nRefCount); - if (m_nRefCount == 0) { + if (nRefCount == 0) { delete this; } return nRefCount;
@@ -244,7 +244,7 @@ STDMETHODIMP_(ULONG) CQGAVssProvider::AddRef() STDMETHODIMP_(ULONG) CQGAVssProvider::Release() { long nRefCount = InterlockedDecrement(&m_nRefCount); - if (m_nRefCount == 0) { + if (nRefCount == 0) { delete this; } return nRefCount;
@@ -477,7 +477,7 @@ STDMETHODIMP_(ULONG) CQGAVssProviderFactory::AddRef() STDMETHODIMP_(ULONG) CQGAVssProviderFactory::Release() { long nRefCount = InterlockedDecrement(&m_nRefCount); - if (m_nRefCount == 0) { + if (nRefCount == 0) { delete this; } return nRefCount;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 02/17] qga/vss-win32: fix race condition in COM Release() methods
Quoted 49 lines · Marc-André Lureau
All three
Release()implementations checked the memberm_nRefCountafter InterlockedDecrement instead of using the local return value. In a multi-threaded context (vssvc), two threads decrementing concurrently could both readm_nRefCountas 0 and double-free.Fixes b39297aedfab ("qemu-ga: Add Windows VSS provider and requester asDLL")
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/provider.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)qga/vss-win32/provider.cpp +3
-4@@ -108,7 +108,7 @@ STDMETHODIMP_(ULONG) CQGAVSSEnumObject::AddRef() STDMETHODIMP_(ULONG) CQGAVSSEnumObject::Release() { long nRefCount = InterlockedDecrement(&m_nRefCount); - if (m_nRefCount == 0) { + if (nRefCount == 0) { delete this; } return nRefCount;@@ -244,7 +244,7 @@ STDMETHODIMP_(ULONG) CQGAVssProvider::AddRef() STDMETHODIMP_(ULONG) CQGAVssProvider::Release() { long nRefCount = InterlockedDecrement(&m_nRefCount); - if (m_nRefCount == 0) { + if (nRefCount == 0) { delete this; } return nRefCount;@@ -477,7 +477,7 @@ STDMETHODIMP_(ULONG) CQGAVssProviderFactory::AddRef() STDMETHODIMP_(ULONG) CQGAVssProviderFactory::Release() { long nRefCount = InterlockedDecrement(&m_nRefCount); - if (m_nRefCount == 0) { + if (nRefCount == 0) { delete this; } return nRefCount; --2.55.0.543.g5ebe2ebe4ea8
PATCH 3/17 qga/vss-win32: pass valid SERVICE_STATUS to ControlService
In reply to Marc-André LureauControlService requires a non-NULL lpServiceStatus parameter ([out] vs [out, optional]).
Passing NULL is undefined behavior per MSDN and can crash on some Windows versions.
--- qga/vss-win32/install.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
qga/vss-win32/install.cpp +2 -1
@@ -572,6 +572,7 @@ namespace _com_util /* Stop QGA VSS provider service using Winsvc API */ STDAPI StopService(void) { + SERVICE_STATUS status; qga_debug_begin; HRESULT hr = S_OK;
@@ -590,7 +591,7 @@ STDAPI StopService(void) hr = E_FAIL; goto out; } - if (!(ControlService(service, SERVICE_CONTROL_STOP, NULL))) { + if (!(ControlService(service, SERVICE_CONTROL_STOP, &status))) { errmsg(E_FAIL, "Failed to stop service"); hr = E_FAIL; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 03/17] qga/vss-win32: pass valid SERVICE_STATUS to ControlService
Quoted 37 lines · Marc-André Lureau
ControlService requires a non-NULL lpServiceStatus parameter ([out] vs [out, optional]).
Passing NULL is undefined behavior per MSDN and can crash on some Windows versions.Fixes 917ebcb17027 ("qga-win: Fix QGA VSS Provider service stop failure")Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/install.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)qga/vss-win32/install.cpp +2
-2@@ -572,6 +572,7 @@ namespace _com_util /* Stop QGA VSS provider service using Winsvc API */ STDAPI StopService(void) { + SERVICE_STATUS status; qga_debug_begin; HRESULT hr = S_OK;@@ -590,7 +591,7 @@ STDAPI StopService(void) hr = E_FAIL; goto out; } - if (!(ControlService(service, SERVICE_CONTROL_STOP, NULL))) { + if (!(ControlService(service, SERVICE_CONTROL_STOP, &status))) { errmsg(E_FAIL, "Failed to stop service"); hr = E_FAIL; } --2.55.0.543.g5ebe2ebe4ea8
PATCH 4/17 qga/vss-win32: explicit NULL/zero initialization
In reply to Marc-André LureauWe use -ftrivial-auto-var-init=zero, but static analyzers don't..
--- qga/vss-win32/install.cpp | 2 +- qga/vss-win32/requester.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
qga/vss-win32/install.cpp +1 -1
@@ -61,7 +61,7 @@ static void errmsg(DWORD err, const char *text) static void errmsg_dialog(DWORD err, const char *text, const char *opt = "") { - char *msg, buf[512]; + char *msg = NULL, buf[512] = ""; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
qga/vss-win32/requester.cpp +1 -1
@@ -170,7 +170,7 @@ static void AddComponents(ErrorSet *errset) unsigned int cComponents, c1, c2, j; COMPointer<IVssExamineWriterMetadata> pMetadata; COMPointer<IVssWMComponent> pComponent; - PVSSCOMPONENTINFO info; + PVSSCOMPONENTINFO info = NULL; HRESULT hr; hr = vss_ctx.pVssbc->GetWriterMetadataCount(&cWriters);
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 04/17] qga/vss-win32: explicit NULL/zero initialization
Quoted 41 lines · Marc-André Lureau
We use -ftrivial-auto-var-init=zero, but static analyzers don't..
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/install.cpp | 2 +- qga/vss-win32/requester.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)qga/vss-win32/install.cpp +1
-1@@ -61,7 +61,7 @@ static void errmsg(DWORD err, const char *text) static void errmsg_dialog(DWORD err, const char *text, const char *opt = "") { - char *msg, buf[512]; + char *msg = NULL, buf[512] = ""; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,qga/vss-win32/requester.cpp +1
-2@@ -170,7 +170,7 @@ static void AddComponents(ErrorSet *errset) unsigned int cComponents, c1, c2, j; COMPointer<IVssExamineWriterMetadata> pMetadata; COMPointer<IVssWMComponent> pComponent; - PVSSCOMPONENTINFO info; + PVSSCOMPONENTINFO info = NULL; HRESULT hr; hr = vss_ctx.pVssbc->GetWriterMetadataCount(&cWriters); --2.55.0.543.g5ebe2ebe4ea8
PATCH 5/17 qga/vss-win32: free bstrWriterName in outer writer loop
In reply to Marc-André LureaubstrWriterName is obtained from GetIdentity in the outer writer loop, but freed inside the inner component loop. When a writer has multiple components, the second and subsequent iterations operate with a NULL bstrWriterName. If an error occurs in those iterations, the error message passes NULL
Move SysFreeString(bstrWriterName) to the end of the outer loop where it belongs.
--- qga/vss-win32/requester.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
qga/vss-win32/requester.cpp +2 -2
@@ -230,11 +230,11 @@ static void AddComponents(ErrorSet *errset) goto out; } } - SysFreeString(bstrWriterName); - bstrWriterName = NULL; pComponent->FreeComponentInfo(info); info = NULL; } + SysFreeString(bstrWriterName); + bstrWriterName = NULL; } out: if (bstrWriterName) {
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 05/17] qga/vss-win32: free bstrWriterName in outer writer loop
Quoted 40 lines · Marc-André Lureau
bstrWriterName is obtained from GetIdentity in the outer writer loop, but freed inside the inner component loop. When a writer has multiple components, the second and subsequent iterations operate with a NULL bstrWriterName. If an error occurs in those iterations, the error message passes NULL
Move SysFreeString(bstrWriterName) to the end of the outer loop where it belongs.
Fixes b39297aedfab ("qemu-ga: Add Windows VSS provider and requester asDLL")
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/requester.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)qga/vss-win32/requester.cpp +2
-3@@ -230,11 +230,11 @@ static void AddComponents(ErrorSet *errset) goto out; } } - SysFreeString(bstrWriterName); - bstrWriterName = NULL; pComponent->FreeComponentInfo(info); info = NULL; } + SysFreeString(bstrWriterName); + bstrWriterName = NULL; } out: if (bstrWriterName) { --2.55.0.543.g5ebe2ebe4ea8
PATCH 6/17 qga/vss-win32: pass character count instead of byte count to volume APIs
In reply to Marc-André LureauFindFirstVolumeW, FindNextVolumeW and GetVolumePathNamesForVolumeNameW expect a buffer length in characters, not bytes.
Use ARRAYSIZE() to pass the correct character count.
--- qga/vss-win32/requester.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
qga/vss-win32/requester.cpp +3 -3
@@ -432,7 +432,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) } if (!mountpoints) { - volume = FindFirstVolumeW(short_volume_name, sizeof(short_volume_name)); + volume = FindFirstVolumeW(short_volume_name, ARRAYSIZE(short_volume_name)); if (volume == INVALID_HANDLE_VALUE) { err_set(errset, hr, "failed to find first volume"); goto out;
@@ -447,7 +447,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) WCHAR volume_path_name[MAX_PATH]; if (GetVolumePathNamesForVolumeNameW( short_volume_name, volume_path_name, - sizeof(volume_path_name), NULL) && + ARRAYSIZE(volume_path_name), NULL) && *volume_path_name) { display_name = volume_path_name; }
@@ -459,7 +459,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) num_fixed_drives++; } if (!FindNextVolumeW(volume, short_volume_name, - sizeof(short_volume_name))) { + ARRAYSIZE(short_volume_name))) { FindVolumeClose(volume); break; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 06/17] qga/vss-win32: pass character count instead of byte count to volume APIs
Quoted 53 lines · Marc-André Lureau
FindFirstVolumeW, FindNextVolumeW and GetVolumePathNamesForVolumeNameW expect a buffer length in characters, not bytes.
Use
ARRAYSIZE()to pass the correct character count.Fixes b39297aedfab ("qemu-ga: Add Windows VSS provider and requester asDLL")
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/requester.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)qga/vss-win32/requester.cpp +1
-1@@ -432,7 +432,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) } if (!mountpoints) { - volume = FindFirstVolumeW(short_volume_name, sizeof(short_volume_name)); + volume = FindFirstVolumeW(short_volume_name, ARRAYSIZE(short_volume_name));if (volume == INVALID_HANDLE_VALUE) { err_set(errset, hr, "failed to find first volume"); goto out;qga/vss-win32/requester.cpp +1
-1@@ -447,7 +447,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) WCHAR volume_path_name[MAX_PATH]; if (GetVolumePathNamesForVolumeNameW( short_volume_name, volume_path_name, - sizeof(volume_path_name), NULL) && + ARRAYSIZE(volume_path_name), NULL) && *volume_path_name) { display_name = volume_path_name;}qga/vss-win32/requester.cpp +1
-1@@ -459,7 +459,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) num_fixed_drives++; } if (!FindNextVolumeW(volume, short_volume_name, - sizeof(short_volume_name))) { + ARRAYSIZE(short_volume_name))) { FindVolumeClose(volume); break;}--
2.55.0.543.g5ebe2ebe4ea8
PATCH 7/17 qga/vss-win32: allow pceltFetched==NULL in IEnumObject::Next
In reply to Marc-André LureauThe COM IEnumXxx/IEnumUnknown::Next API allows callers to pass NULL for pceltFetched when celt is 1. Guard the write to prevent crashing.
--- qga/vss-win32/provider.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
qga/vss-win32/provider.cpp +3 -1
@@ -117,7 +117,9 @@ STDMETHODIMP_(ULONG) CQGAVSSEnumObject::Release() STDMETHODIMP CQGAVSSEnumObject::Next( ULONG celt, VSS_OBJECT_PROP *rgelt, ULONG *pceltFetched) { - *pceltFetched = 0; + if (pceltFetched) { + *pceltFetched = 0; + } return S_FALSE; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 07/17] qga/vss-win32: allow pceltFetched==NULL in IEnumObject::Next
Quoted 30 lines · Marc-André Lureau
The COM IEnumXxx/IEnumUnknown::Next API allows callers to pass NULL for pceltFetched when celt is 1. Guard the write to prevent crashing.
Fixes b39297aedfab ("qemu-ga: Add Windows VSS provider and requester asDLL")
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/provider.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)qga/vss-win32/provider.cpp +3
-2@@ -117,7 +117,9 @@ STDMETHODIMP_(ULONG) CQGAVSSEnumObject::Release() STDMETHODIMP CQGAVSSEnumObject::Next( ULONG celt, VSS_OBJECT_PROP *rgelt, ULONG *pceltFetched) { - *pceltFetched = 0; + if (pceltFetched) { + *pceltFetched = 0; + } return S_FALSE; } --2.55.0.543.g5ebe2ebe4ea8
PATCH 8/17 qga/vss-win32: return an interface pointer for IID_IUnknown
In reply to Marc-André LureauCQGAVssProvider implements three COM interfaces, each with its own IUnknown base subobject. Returning the complete object address for IID_IUnknown relies on IVssSoftwareSnapshotProvider remaining the first base class.
Return the IVssSoftwareSnapshotProvider subobject instead. This selects the canonical IUnknown identity without relying on the complete-object layout.
--- qga/vss-win32/provider.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
qga/vss-win32/provider.cpp +2 -1
@@ -212,7 +212,8 @@ CQGAVssProvider::~CQGAVssProvider() STDMETHODIMP CQGAVssProvider::QueryInterface(REFIID riid, void **ppObj) { if (riid == IID_IUnknown) { - *ppObj = static_cast<void*>(this); + *ppObj = static_cast<void*>( + static_cast<IVssSoftwareSnapshotProvider*>(this)); AddRef(); return S_OK; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 08/17] qga/vss-win32: return an interface pointer for IID_IUnknown
Quoted 33 lines · Marc-André Lureau
CQGAVssProvider implements three COM interfaces, each with its own IUnknown base subobject. Returning the complete object address for
IID_IUnknownrelies on IVssSoftwareSnapshotProvider remaining the first base class.Return the IVssSoftwareSnapshotProvider subobject instead. This selects the canonical IUnknown identity without relying on the complete-object layout.
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/provider.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)qga/vss-win32/provider.cpp +2
-2@@ -212,7 +212,8 @@ CQGAVssProvider::~CQGAVssProvider() STDMETHODIMP CQGAVssProvider::QueryInterface(REFIID riid, void **ppObj) { if (riid == IID_IUnknown) { - *ppObj = static_cast<void*>(this); + *ppObj = static_cast<void*>( + static_cast<IVssSoftwareSnapshotProvider*>(this)); AddRef(); return S_OK; } --2.55.0.543.g5ebe2ebe4ea8
PATCH 9/17 qga/vss-win32: use GetLastError() after FindFirstVolumeW failure
In reply to Marc-André LureauWhen FindFirstVolumeW fails, the error was reported using a stale HRESULT from a prior SetContext call instead of the actual Windows error. Use GetLastError() to report the correct failure reason.
--- qga/vss-win32/requester.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qga/vss-win32/requester.cpp +1 -1
@@ -434,7 +434,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) if (!mountpoints) { volume = FindFirstVolumeW(short_volume_name, ARRAYSIZE(short_volume_name)); if (volume == INVALID_HANDLE_VALUE) { - err_set(errset, hr, "failed to find first volume"); + err_set(errset, GetLastError(), "failed to find first volume"); goto out; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 09/17] qga/vss-win32: use GetLastError() after FindFirstVolumeW failure
Quoted 32 lines · Marc-André Lureau
When FindFirstVolumeW fails, the error was reported using a stale HRESULT from a prior SetContext call instead of the actual Windows error. Use
GetLastError()to report the correct failure reason.Fixes 0692b03ee135 ("qga-win: add support for
qmp_guest_fsfreeze_freeze_list")Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/requester.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qga/vss-win32/requester.cpp +1
-1@@ -434,7 +434,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) if (!mountpoints) { volume = FindFirstVolumeW(short_volume_name, ARRAYSIZE(short_volume_name)); if (volume == INVALID_HANDLE_VALUE) { - err_set(errset, hr, "failed to find first volume"); + err_set(errset, GetLastError(), "failed to find first volume");goto out; }--
2.55.0.543.g5ebe2ebe4ea8
PATCH 10/17 qga/vss-win32: guard CloseServiceHandle calls against NULL handles
In reply to Marc-André LureauStopService() unconditionally calls CloseServiceHandle() on both the service and manager handles in the cleanup path, even when OpenSCManager() or OpenService() failed and the handles are NULL.
--- qga/vss-win32/install.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
qga/vss-win32/install.cpp +6 -2
@@ -597,8 +597,12 @@ STDAPI StopService(void) } out: - CloseServiceHandle(service); - CloseServiceHandle(manager); + if (service) { + CloseServiceHandle(service); + } + if (manager) { + CloseServiceHandle(manager); + } qga_debug_end; return hr; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 10/17] qga/vss-win32: guard CloseServiceHandle calls against NULL handles
Quoted 34 lines · Marc-André Lureau
StopService()unconditionally callsCloseServiceHandle()on both the service and manager handles in the cleanup path, even whenOpenSCManager()orOpenService()failed and the handles are NULL.Fixes 917ebcb17027 ("qga-win: Fix QGA VSS Provider service stop failure")Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/install.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)qga/vss-win32/install.cpp +6
-3@@ -597,8 +597,12 @@ STDAPI StopService(void) } out: - CloseServiceHandle(service); - CloseServiceHandle(manager); + if (service) { + CloseServiceHandle(service); + } + if (manager) { + CloseServiceHandle(manager); + } qga_debug_end; return hr; } --2.55.0.543.g5ebe2ebe4ea8
PATCH 11/17 qga/vss-win32: avoid CRT calls in DllMain debug logging
In reply to Marc-André Lureauqga_debug() uses vsnprintf, snprintf and fputs while DllMain holds the loader lock. CRT I/O can deadlock under the loader lock. Replace with direct OutputDebugStringA calls, which are safe to call from DllMain.
--- qga/vss-win32/provider.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
qga/vss-win32/provider.cpp +2 -2
@@ -536,11 +536,11 @@ BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD dwReason, LPVOID lpReserved); EXTERN_C BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD dwReason, LPVOID lpReserved) { - qga_debug("begin, reason = %lu", dwReason); + OutputDebugStringA(QGA_PROVIDER_NAME ": DllMain begin\n"); if (dwReason == DLL_PROCESS_ATTACH) { g_hinstDll = hinstDll; DisableThreadLibraryCalls(hinstDll); } - qga_debug_end; + OutputDebugStringA(QGA_PROVIDER_NAME ": DllMain end\n"); return TRUE; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 11/17] qga/vss-win32: avoid CRT calls in DllMain debug logging
Quoted 34 lines · Marc-André Lureau
qga_debug()uses vsnprintf, snprintf and fputs while DllMain holds the loader lock. CRT I/O can deadlock under the loader lock. Replace with direct OutputDebugStringA calls, which are safe to call from DllMain.Fixes 61df91b33caf ("QGA VSS: Add log in functions begin/end")Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/provider.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)qga/vss-win32/provider.cpp +2
-2@@ -536,11 +536,11 @@ BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD dwReason, LPVOID lpReserved); EXTERN_C BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD dwReason, LPVOID lpReserved) { - qga_debug("begin, reason = %lu", dwReason); + OutputDebugStringA(QGA_PROVIDER_NAME ": DllMain begin\n"); if (dwReason == DLL_PROCESS_ATTACH) { g_hinstDll = hinstDll; DisableThreadLibraryCalls(hinstDll); } - qga_debug_end; + OutputDebugStringA(QGA_PROVIDER_NAME ": DllMain end\n"); return TRUE;}
--
2.55.0.543.g5ebe2ebe4ea8
PATCH 12/17 qga/vss-win32: evaluate err_set() macro argument only once
In reply to Marc-André LureauThe err_set macro expands its 'err' parameter three times. When callers pass GetLastError(), the Win32 error code is fetched on each expansion and may change between evaluations, since the intervening function calls can reset the thread's last-error value.
Capture the argument in a local DWORD once and reference that throughout the macro body.
--- qga/vss-win32/requester.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
qga/vss-win32/requester.cpp +4 -3
@@ -27,10 +27,11 @@ #define DEFAULT_VSS_BACKUP_TYPE VSS_BT_FULL #define err_set(e, err, fmt, ...) { \ + DWORD _e = (DWORD)(err); \ (e)->error_setg_win32_wrapper((e)->errp, __FILE__, __LINE__, __func__, \ - err, fmt ": Windows error 0x%lx", \ - ## __VA_ARGS__, err); \ - qga_debug(fmt ": Windows error 0x%lx", ## __VA_ARGS__, err); \ + _e, fmt ": Windows error 0x%lx", \ + ## __VA_ARGS__, _e); \ + qga_debug(fmt ": Windows error 0x%lx", ## __VA_ARGS__, _e); \ } /* Bad idea, works only when (e)->errp != NULL: */ #define err_is_set(e) ((e)->errp && *(e)->errp)
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 12/17] qga/vss-win32: evaluate err_set() macro argument only once
Quoted 46 lines · Marc-André Lureau
The
err_setmacro expands its 'err' parameter three times. When callers passGetLastError(), the Win32 error code is fetched on each expansion and may change between evaluations, since the intervening function calls can reset the thread's last-error value.Capture the argument in a local DWORD once and reference that throughout the macro body.
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/requester.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)qga/vss-win32/requester.cpp +4
-4@@ -27,10 +27,11 @@ #define DEFAULT_VSS_BACKUP_TYPE VSS_BT_FULL #define err_set(e, err, fmt, ...) { \ + DWORD _e = (DWORD)(err); \ (e)->error_setg_win32_wrapper((e)->errp, __FILE__, __LINE__, __func__, \ - err, fmt ": Windows error 0x%lx", \ - ## __VA_ARGS__, err); \ - qga_debug(fmt ": Windows error 0x%lx", ## __VA_ARGS__, err); \ + _e, fmt ": Windows error 0x%lx", \ + ## __VA_ARGS__, _e); \ + qga_debug(fmt ": Windows error 0x%lx", ## __VA_ARGS__, _e); \ } /* Bad idea, works only when (e)->errp != NULL: */ #define err_is_set(e) ((e)->errp && *(e)->errp) --2.55.0.543.g5ebe2ebe4ea8
PATCH 13/17 qga/vss-win32: report QueryStatus return value on failure
In reply to Marc-André LureauWhen IVssAsync::QueryStatus fails, the error was reported using its output parameter hr rather than its return value hr2. Since the output parameter is not guaranteed to be written on failure, this could report a stale or uninitialized HRESULT, masking the actual error.
--- qga/vss-win32/requester.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qga/vss-win32/requester.cpp +1 -1
@@ -506,7 +506,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) for (i = 0; i < VSS_TIMEOUT_FREEZE_MSEC/VSS_TIMEOUT_EVENT_MSEC; i++) { HRESULT hr2 = vss_ctx.pAsyncSnapshot->QueryStatus(&hr, NULL); if (FAILED(hr2)) { - err_set(errset, hr, "failed to do snapshot set"); + err_set(errset, hr2, "failed to query snapshot set status"); goto out; } if (hr != VSS_S_ASYNC_PENDING) {
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 13/17] qga/vss-win32: report QueryStatus return value on failure
Quoted 31 lines · Marc-André Lureau
When
IVssAsync::QueryStatusfails, the error was reported using its output parameterhrrather than its return valuehr2. Since the output parameter is not guaranteed to be written on failure, this could report a stale or uninitialized HRESULT, masking the actual error.Fixes b39297aedfab ("qemu-ga: Add Windows VSS provider and requester asDLL")
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/requester.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)qga/vss-win32/requester.cpp +1
-1@@ -506,7 +506,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) for (i = 0; i < VSS_TIMEOUT_FREEZE_MSEC/VSS_TIMEOUT_EVENT_MSEC; i++) { HRESULT hr2 = vss_ctx.pAsyncSnapshot->QueryStatus(&hr, NULL); if (FAILED(hr2)) { - err_set(errset, hr, "failed to do snapshot set"); + err_set(errset, hr2, "failed to query snapshot set status"); goto out; }if (hr != VSS_S_ASYNC_PENDING) {--
2.55.0.543.g5ebe2ebe4ea8
PATCH 14/17 qga/vss-win32: check GetLastError() after FindNextVolumeW failure
In reply to Marc-André LureauFindNextVolumeW returning FALSE can mean either normal end of enumeration (ERROR_NO_MORE_FILES) or an actual error. The code treated both cases as end-of-list, silently skipping any remaining volumes on a real failure.
Distinguish the two by checking GetLastError().
--- qga/vss-win32/requester.cpp | 7 +++++++ 1 file changed, 7 insertions(+)
qga/vss-win32/requester.cpp +7 -0
@@ -461,6 +461,13 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) } if (!FindNextVolumeW(volume, short_volume_name, ARRAYSIZE(short_volume_name))) { + DWORD err = GetLastError(); + if (err != ERROR_NO_MORE_FILES) { + err_set(errset, err, + "failed to find next volume"); + FindVolumeClose(volume); + goto out; + } FindVolumeClose(volume); break; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 14/17] qga/vss-win32: check GetLastError() after FindNextVolumeW failure
Quoted 38 lines · Marc-André Lureau
FindNextVolumeW returning FALSE can mean either normal end of enumeration (
ERROR_NO_MORE_FILES) or an actual error. The code treated both cases as end-of-list, silently skipping any remaining volumes on a real failure.Distinguish the two by checking
GetLastError().Fixes b39297aedfab ("qemu-ga: Add Windows VSS provider and requester asDLL")
Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/requester.cpp | 7 +++++++ 1 file changed, 7 insertions(+)qga/vss-win32/requester.cpp +7
-0@@ -461,6 +461,13 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) } if (!FindNextVolumeW(volume, short_volume_name, ARRAYSIZE(short_volume_name))) { + DWORD err = GetLastError(); + if (err != ERROR_NO_MORE_FILES) { + err_set(errset, err, + "failed to find next volume"); + FindVolumeClose(volume); + goto out; + } FindVolumeClose(volume); break;}--
2.55.0.543.g5ebe2ebe4ea8
PATCH 15/17 qga/vss-win32: initialize *num_vols at start of requester_freeze
In reply to Marc-André LureauInitialize *num_vols to 0 at the top of requester_freeze() so all error paths and zero-volume early exits return a defined value to the caller. Previously, only the already-frozen path and the success path set *num_vols; every goto-out error path left it indeterminate.
The sole caller is qga_vss_freeze(), which already sets *nr_volume = 0.
--- qga/vss-win32/requester.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
qga/vss-win32/requester.cpp +2 -1
@@ -306,8 +306,9 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) int num_mount_points = 0; VSS_BACKUP_TYPE vss_bt = get_vss_backup_type(); + *num_vols = 0; + if (vss_ctx.pVssbc) { /* already frozen */ - *num_vols = 0; qga_debug("finished, already frozen"); return; }
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 15/17] qga/vss-win32: initialize *num_vols at start of requester_freeze
Quoted 33 lines · Marc-André Lureau
Initialize
*num_volsto 0 at the top ofrequester_freeze()so all error paths and zero-volume early exits return a defined value to the caller. Previously, only the already-frozen path and the success path set*num_vols; every goto-out error path left it indeterminate.The sole caller is
qga_vss_freeze(), which already sets*nr_volume= 0.Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/requester.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)qga/vss-win32/requester.cpp +2
-1@@ -306,8 +306,9 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) int num_mount_points = 0; VSS_BACKUP_TYPE vss_bt = get_vss_backup_type(); + *num_vols = 0; + if (vss_ctx.pVssbc) { /* already frozen */ - *num_vols = 0; qga_debug("finished, already frozen"); return;}--
2.55.0.543.g5ebe2ebe4ea8
PATCH 16/17 qga/vss-win32: restrict access to freeze/thaw synchronisation events
In reply to Marc-André LureauThe freeze, thaw and timeout events were created with a NULL DACL, granting unrestricted access. Any local process could open and signal them, allowing an unprivileged guest process to trigger a filesystem freeze or premature thaw.
Replace the NULL DACL with an explicit ACL that grants EVENT_ALL_ACCESS only to SYSTEM and the built-in Administrators group, which are the accounts under which the VSS service and qemu-ga operate.
--- qga/vss-win32/requester.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-)
qga/vss-win32/requester.cpp +49 -3
@@ -17,6 +17,7 @@ #include "install.h" #include <vswriter.h> #include <vsbackup.h> +#include <aclapi.h> /* Max wait time for frozen event (VSS can only hold writes for 10 seconds) */ #define VSS_TIMEOUT_FREEZE_MSEC 60000
@@ -298,10 +299,14 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) HRESULT hr; LONG ctx; GUID guidSnapshotSet = GUID_NULL; + SID_IDENTIFIER_AUTHORITY sia_nt = SECURITY_NT_AUTHORITY; + PSID pSidSystem = NULL, pSidAdmins = NULL; SECURITY_DESCRIPTOR sd; SECURITY_ATTRIBUTES sa; + EXPLICIT_ACCESS ea[2]; + PACL pAcl = NULL; WCHAR short_volume_name[64], *display_name = short_volume_name; - DWORD wait_status; + DWORD wait_status, aclResult; int num_fixed_drives = 0, i; int num_mount_points = 0; VSS_BACKUP_TYPE vss_bt = get_vss_backup_type();
@@ -313,9 +318,48 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) return; } - /* Allow unrestricted access to events */ + /* Grant access to SYSTEM and Administrators only */ + if (!AllocateAndInitializeSid(&sia_nt, 1, + SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, + &pSidSystem)) { + err_set(errset, GetLastError(), "failed to create SYSTEM SID"); + goto out; + } + if (!AllocateAndInitializeSid(&sia_nt, 2, + SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, + 0, 0, 0, 0, 0, 0, &pSidAdmins)) { + DWORD err = GetLastError(); + FreeSid(pSidSystem); + err_set(errset, err, "failed to create Administrators SID"); + goto out; + } + + ZeroMemory(&ea, sizeof(ea)); + ea[0].grfAccessPermissions = EVENT_ALL_ACCESS; + ea[0].grfAccessMode = SET_ACCESS; + ea[0].grfInheritance = NO_INHERITANCE; + ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; + ea[0].Trustee.ptstrName = (LPTSTR)pSidSystem; + ea[1].grfAccessPermissions = EVENT_ALL_ACCESS; + ea[1].grfAccessMode = SET_ACCESS; + ea[1].grfInheritance = NO_INHERITANCE; + ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea[1].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; + ea[1].Trustee.ptstrName = (LPTSTR)pSidAdmins; + + aclResult = SetEntriesInAcl(2, ea, NULL, &pAcl); + if (aclResult != ERROR_SUCCESS) { + FreeSid(pSidSystem); + FreeSid(pSidAdmins); + err_set(errset, aclResult, "failed to create ACL for events"); + goto out; + } + FreeSid(pSidSystem); + FreeSid(pSidAdmins); + InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); - SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE); + SetSecurityDescriptorDacl(&sd, TRUE, pAcl, FALSE); sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = &sd; sa.bInheritHandle = FALSE;
@@ -551,6 +595,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) } qga_debug("end successful"); + LocalFree(pAcl); return; out:
@@ -559,6 +604,7 @@ out: } out1: + LocalFree(pAcl); requester_cleanup(); qga_debug_end;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 16/17] qga/vss-win32: restrict access to freeze/thaw synchronisation events
Quoted 119 lines · Marc-André Lureau
The freeze, thaw and timeout events were created with a NULL DACL, granting unrestricted access. Any local process could open and signal them, allowing an unprivileged guest process to trigger a filesystem freeze or premature thaw.
Replace the NULL DACL with an explicit ACL that grants
EVENT_ALL_ACCESSonly to SYSTEM and the built-in Administrators group, which are the accounts under which the VSS service and qemu-ga operate.Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/requester.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-)qga/vss-win32/requester.cpp +1
-0@@ -17,6 +17,7 @@ #include "install.h" #include <vswriter.h> #include <vsbackup.h> +#include <aclapi.h> /* Max wait time for frozen event (VSS can only hold writes for 10seconds) */
#defineVSS_TIMEOUT_FREEZE_MSEC60000qga/vss-win32/requester.cpp +5
-1@@ -298,10 +299,14 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) HRESULT hr; LONG ctx; GUID guidSnapshotSet = GUID_NULL; + SID_IDENTIFIER_AUTHORITY sia_nt = SECURITY_NT_AUTHORITY; + PSID pSidSystem = NULL, pSidAdmins = NULL; SECURITY_DESCRIPTOR sd; SECURITY_ATTRIBUTES sa; + EXPLICIT_ACCESS ea[2]; + PACL pAcl = NULL; WCHAR short_volume_name[64], *display_name = short_volume_name; - DWORD wait_status; + DWORD wait_status, aclResult; int num_fixed_drives = 0, i; int num_mount_points = 0;VSS_BACKUP_TYPE vss_bt = get_vss_backup_type();qga/vss-win32/requester.cpp +41
-2@@ -313,9 +318,48 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) return; } - /* Allow unrestricted access to events */ + /* Grant access to SYSTEM and Administrators only */ + if (!AllocateAndInitializeSid(&sia_nt, 1, + SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, + &pSidSystem)) { + err_set(errset, GetLastError(), "failed to create SYSTEM SID"); + goto out; + } + if (!AllocateAndInitializeSid(&sia_nt, 2, + SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, + 0, 0, 0, 0, 0, 0, &pSidAdmins)) { + DWORD err = GetLastError(); + FreeSid(pSidSystem); + err_set(errset, err, "failed to create Administrators SID"); + goto out; + } + + ZeroMemory(&ea, sizeof(ea)); + ea[0].grfAccessPermissions = EVENT_ALL_ACCESS; + ea[0].grfAccessMode = SET_ACCESS; + ea[0].grfInheritance = NO_INHERITANCE; + ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; + ea[0].Trustee.ptstrName = (LPTSTR)pSidSystem; + ea[1].grfAccessPermissions = EVENT_ALL_ACCESS; + ea[1].grfAccessMode = SET_ACCESS; + ea[1].grfInheritance = NO_INHERITANCE; + ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea[1].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; + ea[1].Trustee.ptstrName = (LPTSTR)pSidAdmins; + + aclResult = SetEntriesInAcl(2, ea, NULL, &pAcl); + if (aclResult != ERROR_SUCCESS) { + FreeSid(pSidSystem); + FreeSid(pSidAdmins); + err_set(errset, aclResult, "failed to create ACL for events"); + goto out; + } + FreeSid(pSidSystem); + FreeSid(pSidAdmins); + InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION); - SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE); + SetSecurityDescriptorDacl(&sd, TRUE, pAcl, FALSE); sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = &sd;sa.bInheritHandle = FALSE;qga/vss-win32/requester.cpp +1
-0@@ -551,6 +595,7 @@ void requester_freeze(int *num_vols, void *mountpoints, ErrorSet *errset) } qga_debug("end successful"); + LocalFree(pAcl); return;out:
qga/vss-win32/requester.cpp +1
-1@@ -559,6 +604,7 @@ out: } out1: + LocalFree(pAcl); requester_cleanup(); qga_debug_end; --2.55.0.543.g5ebe2ebe4ea8
PATCH 17/17 qga/vss-win32: log to file during COM+ registration
In reply to Marc-André LureauCOMRegister and COMUnregister are invoked both from the MSI installer (via DLLCOMRegister/rundll32) and from qemu-ga -s vss-install, where stderr is typically not visible. Redirect stderr to %SystemRoot%\Temp\qga-vss-install.log so that qga_debug output is captured for post-install troubleshooting.
--- qga/vss-win32/install.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
qga/vss-win32/install.cpp +18 -0
@@ -241,10 +241,27 @@ out: return hr; } +static void qga_vss_open_log(void) +{ + char path[MAX_PATH]; + UINT n; + + n = GetSystemWindowsDirectoryA(path, sizeof(path)); + if (n == 0 || n >= sizeof(path)) { + return; + } + snprintf(path + n, sizeof(path) - n, + "\\Temp\\qga-vss-install.log"); + if (freopen(path, "a", stderr)) { + setvbuf(stderr, NULL, _IONBF, 0); + } +} + /* Unregister this module from COM+ Applications Catalog */ STDAPI COMUnregister(void); STDAPI COMUnregister(void) { + qga_vss_open_log(); qga_debug_begin; HRESULT hr;
@@ -260,6 +277,7 @@ out: STDAPI COMRegister(void); STDAPI COMRegister(void) { + qga_vss_open_log(); qga_debug_begin; HRESULT hr;
Signature · 1 line
2.55.0.543.g5ebe2ebe4ea8
Re: [PATCH 17/17] qga/vss-win32: log to file during COM+ registration
Quoted 57 lines · Marc-André Lureau
COMRegister and COMUnregister are invoked both from the MSI installer (via DLLCOMRegister/rundll32) and from qemu-ga -s vss-install, where stderr is typically not visible. Redirect stderr to %SystemRoot%\Temp\qga-vss-install.log so that
qga_debugoutput is captured for post-install troubleshooting.Suggested-by Konstantin Kostiuk <[email protected]>Signed-off-by Marc-André Lureau <[email protected]>--- qga/vss-win32/install.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)qga/vss-win32/install.cpp +18
-1@@ -241,10 +241,27 @@ out: return hr; } +static void qga_vss_open_log(void) +{ + char path[MAX_PATH]; + UINT n; + + n = GetSystemWindowsDirectoryA(path, sizeof(path)); + if (n == 0 || n >= sizeof(path)) { + return; + } + snprintf(path + n, sizeof(path) - n, + "\\Temp\\qga-vss-install.log"); + if (freopen(path, "a", stderr)) { + setvbuf(stderr, NULL, _IONBF, 0); + } +} + /* Unregister this module from COM+ Applications Catalog */ STDAPI COMUnregister(void); STDAPI COMUnregister(void) { + qga_vss_open_log(); qga_debug_begin; HRESULT hr;@@ -260,6 +277,7 @@ out: STDAPI COMRegister(void); STDAPI COMRegister(void) { + qga_vss_open_log(); qga_debug_begin; HRESULT hr; --2.55.0.543.g5ebe2ebe4ea8