From 049e28ca02377ce5d47d3c085246ebee0954323d Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Wed, 16 Jul 2025 14:40:28 -0400 Subject: [PATCH] For 29994: Use comshim for proper COM initialization (#30920) For #29994 The `mdm_bridge` Orbit table was not using comshim for initializing the multi-threaded COM apartment which was causing panics. --- changes/29994-use-comshim | 1 + orbit/pkg/table/mdm/mdm_windows.go | 17 +++++++---------- 2 files changed, 8 insertions(+), 10 deletions(-) create mode 100644 changes/29994-use-comshim diff --git a/changes/29994-use-comshim b/changes/29994-use-comshim new file mode 100644 index 0000000000..1916131f76 --- /dev/null +++ b/changes/29994-use-comshim @@ -0,0 +1 @@ +* Fixed bug with `mdm_bridge` Orbit table that caused panics due to invalid COM initialization. \ No newline at end of file diff --git a/orbit/pkg/table/mdm/mdm_windows.go b/orbit/pkg/table/mdm/mdm_windows.go index 252d1d33e1..fc927c3214 100644 --- a/orbit/pkg/table/mdm/mdm_windows.go +++ b/orbit/pkg/table/mdm/mdm_windows.go @@ -10,6 +10,7 @@ import ( "encoding/xml" "errors" "fmt" + "github.com/scjalliance/comshim" "io" "os" "strings" @@ -359,6 +360,9 @@ func executeMDMcommand(inputCMD string) (string, error) { mu.Lock() defer mu.Unlock() + comshim.Add(1) + defer comshim.Done() + // checking if input MDM command is valid if validCommand, err := isValidMDMcommand(inputCMD); !validCommand { return "", err @@ -550,16 +554,9 @@ func enableCmdExecution() error { // initialize MDM stack management by generating SHA256 hash of SMBIOS UUID and calling RegisterDeviceWithLocalManagement() // this is wrapped by sync.Once so it only executes once mdmManagementStackInit.Do(func() { - // making sure that COM is initialized - // this is a best effort call as COM stack could have been initialized already by other components - err := windows.CoInitializeEx(0, windows.COINIT_MULTITHREADED) - if err != nil { - log.Error().Msgf("there was an error calling CoInitializeEx(): (%s)", err) - } - - // calling RegisterDeviceWithLocalManagement() to initialize the MDM stack - // The code below is just using returnCode to determine if call was successul or not. The err - // variable returns status above call dispatching so it not needed and actually introduce + // calling RegisterDeviceWithLocalManagement() to initialize the MDM stack, + // The code below is just using returnCode to determine if the call was successful or not. The err + // variable returns the status above call dispatching, so it is unnecessary and actually introduces // confusion about the status of the call. // This is a best effort call as MDM management stack could have been initialized already by other components if returnCode, _, _ := procRegisterDeviceWithLocalManagement.Call(uintptr(unsafe.Pointer(nil))); returnCode != uintptr(windows.ERROR_SUCCESS) {