feat(inputs.win_service): Reduce required rights to GENERIC_READ (#14073)

This commit is contained in:
Sven Rebhan 2023-10-16 16:34:15 +02:00 committed by GitHub
parent efe7112ef9
commit 2a6b16e0ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 2 deletions

View File

@ -8,7 +8,9 @@ import (
"errors"
"fmt"
"io/fs"
"syscall"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
@ -67,8 +69,17 @@ func (m *WinSvcMgr) Disconnect() error {
}
func (m *WinSvcMgr) OpenService(name string) (WinService, error) {
return m.realMgr.OpenService(name)
serviceName, err := syscall.UTF16PtrFromString(name)
if err != nil {
return nil, fmt.Errorf("cannot convert service name %q: %w", name, err)
}
h, err := windows.OpenService(m.realMgr.Handle, serviceName, windows.GENERIC_READ)
if err != nil {
return nil, err
}
return &mgr.Service{Name: name, Handle: h}, nil
}
func (m *WinSvcMgr) ListServices() ([]string, error) {
return m.realMgr.ListServices()
}
@ -78,10 +89,11 @@ type MgProvider struct {
}
func (rmr *MgProvider) Connect() (WinServiceManager, error) {
scmgr, err := mgr.Connect()
h, err := windows.OpenSCManager(nil, nil, windows.GENERIC_READ)
if err != nil {
return nil, err
}
scmgr := &mgr.Mgr{Handle: h}
return &WinSvcMgr{scmgr}, nil
}