Skip to content

Commit 4e7f1d9

Browse files
committed
fix(systray): Use default named pipe security descriptor and small refactoring
The default named pipe security descriptor grants full access to LocalSystem account, administrators, and the creator owner.
1 parent c386ba9 commit 4e7f1d9

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

cmd/systray/main_windows.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"io"
3535
"net"
3636
"os"
37-
"os/user"
3837
"path/filepath"
3938
"unsafe"
4039
)
@@ -238,37 +237,30 @@ func main() {
238237
os.Exit(1)
239238
}
240239
logrus.Info("starting systray server...")
241-
usr, err := user.Current()
242-
if err != nil {
243-
logrus.Fatalf("failed to retrieve the current user: %v", err)
244-
}
245-
// Named pipe security and access rights.
246-
// Give generic read/write access to the
247-
// current user SID
248-
descriptor := "D:P(A;;GA;;;" + usr.Uid + ")"
249240
// spin up the named-pipe server
250-
l, err := winio.ListenPipe(systrayPipe, &winio.PipeConfig{SecurityDescriptor: descriptor})
241+
l, err := winio.ListenPipe(systrayPipe, nil)
251242
if err != nil {
252243
logrus.Fatalf("unable to listen on named pipe: %s: %v", systrayPipe, err)
253244
}
254245

255-
// detach console
256-
sys.FreeConsole()
257-
258246
tray, err := newSystray()
259247
if err != nil {
260248
logrus.Fatalf("unable to create systray: %v", err)
261249
}
262250

263251
go func() {
264252
<-tray.quit
253+
logrus.Info("shutting down...")
265254
l.Close()
266255
err := tray.shutdown()
267256
if err != nil {
268257
logrus.Warnf("fail to shutdown: %v", err)
269258
}
270259
}()
271260

261+
// detach console
262+
sys.FreeConsole()
263+
272264
// server loop
273265
for {
274266
conn, err := l.Accept()

pkg/alertsender/systray/systray.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/Microsoft/go-winio"
2626
"github.com/cenkalti/backoff/v4"
2727
"github.com/rabbitstack/fibratus/pkg/alertsender"
28+
"github.com/rabbitstack/fibratus/pkg/kevent"
2829
log "github.com/sirupsen/logrus"
2930
"os"
3031
"time"
@@ -111,26 +112,29 @@ func makeSender(config alertsender.Config) (alertsender.Sender, error) {
111112
break
112113
}
113114

114-
return s, s.writePipe(&Msg{Type: Conf, Data: c})
115+
return s, s.send(&Msg{Type: Conf, Data: c})
115116
}
116117

117118
func (s *systray) Send(alert alertsender.Alert) error {
118-
return s.writePipe(&Msg{Type: Balloon, Data: alert})
119+
// remove all events to avoid decoding errors on systray server end
120+
alert.Events = make([]*kevent.Kevent, 0)
121+
return s.send(&Msg{Type: Balloon, Data: alert})
119122
}
120123

121124
func (*systray) Type() alertsender.Type { return alertsender.Systray }
122125
func (*systray) SupportsMarkdown() bool { return false }
123126

124127
func (s *systray) Shutdown() error { return nil }
125128

126-
func (s *systray) writePipe(m *Msg) error {
129+
func (s *systray) send(m *Msg) error {
127130
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
128131
defer cancel()
129132
conn, err := winio.DialPipeContext(ctx, systrayPipe)
130133
if err != nil {
131134
return fmt.Errorf("unable to dial %s pipe: %v", systrayPipe, err)
132135
}
133136
defer conn.Close()
137+
134138
b, err := m.encode()
135139
if err != nil {
136140
return err
@@ -141,10 +145,12 @@ func (s *systray) writePipe(m *Msg) error {
141145
if _, err = conn.Write(b); err != nil {
142146
return fmt.Errorf("unable to write systray pipe: %v", err)
143147
}
148+
144149
return nil
145150
}
146151

147152
func pipeExists() bool {
148153
_, err := os.Stat(systrayPipe)
154+
log.Warnf("pipe not found: %v", err)
149155
return err == nil
150156
}

0 commit comments

Comments
 (0)