Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions plugins/inputs/win_services/win_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()
}
Expand All @@ -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
}

Expand Down
Loading