-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
750 lines (678 loc) · 18.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
package main
import (
"context"
"fmt"
"net"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"sync"
"syscall"
"time"
"github.com/awnumar/memguard"
"github.com/inconshreveable/log15"
"github.com/kardianos/osext"
"github.com/spf13/pflag"
"github.com/stephane-martin/skewer/cmd"
"github.com/stephane-martin/skewer/services"
"github.com/stephane-martin/skewer/services/base"
"github.com/stephane-martin/skewer/sys"
"github.com/stephane-martin/skewer/sys/binder"
"github.com/stephane-martin/skewer/sys/capabilities"
"github.com/stephane-martin/skewer/sys/dumpable"
"github.com/stephane-martin/skewer/sys/kring"
"github.com/stephane-martin/skewer/sys/namespaces"
"github.com/stephane-martin/skewer/sys/scomp"
"github.com/stephane-martin/skewer/utils"
"github.com/stephane-martin/skewer/utils/eerrors"
"github.com/stephane-martin/skewer/utils/logging"
)
func fatalError(msg string, err error) error {
msg = strings.TrimSpace(msg)
if len(msg) == 0 && err == nil {
return nil
}
if len(msg) == 0 {
return eerrors.WithTypes(err, "Fatal")
}
if err == nil {
return eerrors.WithTypes(eerrors.New(msg), "Fatal")
}
return eerrors.WithTypes(eerrors.Wrap(err, msg), "Fatal")
}
type spair struct {
child uintptr
parent uintptr
}
func getSocketPair(typ int) (spair, error) {
a, b, err := sys.SocketPair(typ)
if err != nil {
return spair{}, eerrors.Wrap(err, "socketpair error")
}
return spair{child: uintptr(a), parent: uintptr(b)}, nil
}
func getLoggerConn(handle uintptr) (loggerConn *net.UnixConn) {
c, _ := net.FileConn(os.NewFile(handle, "logger"))
conn := c.(*net.UnixConn)
_ = conn.SetReadBuffer(65536)
_ = conn.SetWriteBuffer(65536)
return conn
}
func getLogger(ctx context.Context, name string, secret *memguard.LockedBuffer, handle uintptr) (log15.Logger, error) {
conn, err := net.FileConn(os.NewFile(handle, "logger"))
if err != nil {
return nil, eerrors.Wrap(err, "getLogger failed")
}
loggerConn := conn.(*net.UnixConn)
_ = loggerConn.SetReadBuffer(65536)
_ = loggerConn.SetWriteBuffer(65536)
return logging.NewRemoteLogger(ctx, loggerConn, secret).New("proc", name), nil
}
type reExecType int
const (
earlyCapsDrop reExecType = iota
netBindCapDrop
parentCapsDrop
)
func reExec(t reExecType) error {
switch t {
case earlyCapsDrop:
if capabilities.CapabilitiesSupported {
runtime.LockOSThread()
// very early we drop most of linux capabilities
applied, err := capabilities.Predrop()
if err != nil {
return fatalError("Error pre-dropping capabilities", err)
}
if applied {
exe, err := osext.Executable()
if err != nil {
return fatalError("Error getting executable path", err)
}
err = syscall.Exec(exe, os.Args, os.Environ())
if err != nil {
return fatalError("Error re-executing self", err)
}
}
return nil
}
case netBindCapDrop:
if capabilities.CapabilitiesSupported {
// another execve is necessary on Linux to ensure that
// the following capability drop will be effective on
// all go threads
runtime.LockOSThread()
err := capabilities.DropNetBind()
if err != nil {
return fatalError("error dropping netbind caps", err)
}
exe, err := osext.Executable()
if err != nil {
return fatalError("Error getting executable name", err)
}
err = syscall.Exec(exe, append([]string{"skewer-linux-child"}, os.Args[1:]...), os.Environ())
if err != nil {
return fatalError("Error reexecuting self", err)
}
}
return nil
case parentCapsDrop:
if capabilities.CapabilitiesSupported {
// under Linux, re-exec ourself immediately with fewer privileges
runtime.LockOSThread()
_, err := parseCobraFlags()
if err != nil && err != pflag.ErrHelp {
return fatalError("Error parsing flags", err)
}
needFix, err := capabilities.NeedFixLinuxPrivileges(cmd.UidFlag, cmd.GidFlag)
if err != nil {
return fatalError("Error dropping privileges", err)
}
if needFix {
err = capabilities.FixLinuxPrivileges(cmd.UidFlag, cmd.GidFlag)
if err != nil {
return fatalError("Error dropping privileges", err)
}
err = capabilities.NoNewPriv()
if err != nil {
return fatalError("NoNewPriv error", err)
}
exe, err := os.Executable()
if err != nil {
return fatalError("Error finding executable name", err)
}
err = syscall.Exec(exe, append([]string{"skewer-parent-dropped"}, os.Args[1:]...), os.Environ())
if err != nil {
return fatalError("Error reexecuting self", err)
}
}
}
return nil
}
return fatalError("Unknown reExec type", nil)
}
func execChild() error {
err := scomp.SetupSeccomp(-1)
if err != nil {
return fatalError("Error setting up seccomp", err)
}
err = scomp.SetupPledge(-1)
if err != nil {
return fatalError("Error setting up pledge", err)
}
_, err = parseCobraFlags()
if err != nil && err != pflag.ErrHelp {
return fatalError("Error parsing flags", err)
}
return cmd.ExecuteChild()
}
func execServeParent() error {
/*
err := capabilities.NoNewPriv()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
*/
rootlogger, err := logging.SetupLogging(nil, cmd.LoglevelFlag, cmd.LogjsonFlag, cmd.SyslogFlag, cmd.LogfilenameFlag)
if err != nil {
return fatalError("Error when setting up main logger", err)
}
logger := rootlogger.New("proc", "parent")
ring, err := kring.NewRing()
if err != nil {
return fatalError("Can't make a new keyring", err)
}
boxsecret, err := ring.NewBoxSecret()
if err != nil {
return fatalError("Can't create a new box secret", err)
}
defer func() {
_ = ring.DeleteBoxSecret()
_ = ring.Destroy()
}()
if !cmd.DumpableFlag {
err := dumpable.SetNonDumpable()
if err != nil {
return fatalError("Error setting PR_SET_DUMPABLE", err)
}
}
numuid, numgid, err := sys.LookupUid(cmd.UidFlag, cmd.GidFlag)
if err != nil {
return fatalError("Error looking up uid", err)
}
if numuid == 0 {
return fatalError("Provide a non-privileged user with --uid flag", nil)
}
binderSockets := map[string]spair{}
loggerSockets := map[string]spair{}
for _, h := range base.Handles {
if h.Type == base.Binder {
binderSockets[h.Service], err = getSocketPair(syscall.SOCK_STREAM)
} else {
loggerSockets[h.Service], err = getSocketPair(syscall.SOCK_DGRAM)
}
if err != nil {
return fatalError("Can't create the required socketpairs", err)
}
}
binderParents := []uintptr{}
for _, s := range binderSockets {
binderParents = append(binderParents, s.parent)
}
binderCtx, binderCancel := context.WithCancel(context.Background())
binderWg, err := binder.Server(binderCtx, binderParents, boxsecret, logger) // returns immediately
if err != nil {
return fatalError("Error setting the root binder", err)
}
defer func() {
binderCancel()
binderWg.Wait()
}()
remoteLoggerConn := []*net.UnixConn{}
for _, s := range loggerSockets {
remoteLoggerConn = append(remoteLoggerConn, getLoggerConn(s.parent))
}
logger.Debug("Receiving from remote loggers", "nb", len(remoteLoggerConn))
loggingWg := logging.LogReceiver(loggerCtx, boxsecret, rootlogger, remoteLoggerConn)
defer func() {
cancelLogger()
loggingWg.Wait()
}()
logger.Debug("Target user", "uid", numuid, "gid", numgid)
// execute child under the new user
exe, err := osext.Executable() // custom Executable() function to support OpenBSD
if err != nil {
return fatalError("Error getting executable name", err)
}
extraFiles := []*os.File{}
for _, h := range base.Handles {
if h.Type == base.Binder {
extraFiles = append(extraFiles, os.NewFile(binderSockets[h.Service].child, h.Service))
} else {
extraFiles = append(extraFiles, os.NewFile(loggerSockets[h.Service].child, h.Service))
}
}
rRingSecretPipe, wRingSecretPipe, err := os.Pipe()
if err != nil {
return fatalError("Error creating ring-secret pipe", err)
}
extraFiles = append(extraFiles, rRingSecretPipe)
// the dead man pipe is used by the child to detect that the parent had disappeared
rDeadManPipe, wDeadManPipe, err := os.Pipe()
if err != nil {
return fatalError("Error creating dead man pipe", err)
}
extraFiles = append(extraFiles, rDeadManPipe)
childProcess := exec.Cmd{
Args: append([]string{"skewer-child"}, os.Args[1:]...),
Path: exe,
Stdin: nil,
Stdout: os.Stdout,
Stderr: os.Stderr,
ExtraFiles: extraFiles,
Env: []string{"PATH=/bin:/usr/bin", fmt.Sprintf("SKEWER_SESSION=%s", ring.GetSessionID().String())},
}
if os.Getuid() != numuid {
// execute the child with the given uid, gid
childProcess.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{
Uid: uint32(numuid),
Gid: uint32(numgid),
},
}
}
err = childProcess.Start()
_ = rRingSecretPipe.Close()
_ = rDeadManPipe.Close()
if err != nil {
return fatalError("Error starting child", err)
}
for _, h := range base.Handles {
if h.Type == base.Binder {
_ = syscall.Close(int(binderSockets[h.Service].child))
} else {
_ = syscall.Close(int(loggerSockets[h.Service].child))
}
}
_ = ring.WriteRingPass(wRingSecretPipe)
_ = wRingSecretPipe.Close()
sigChan := make(chan os.Signal, 10)
once := sync.Once{}
go func() {
for sig := range sigChan {
logger.Debug("parent received signal", "signal", sig)
switch sig {
case syscall.SIGTERM:
once.Do(func() { _ = childProcess.Process.Signal(sig) })
return
case syscall.SIGHUP:
// reload configuration
_ = childProcess.Process.Signal(sig)
case syscall.SIGUSR1:
// log rotation
logging.SetupLogging(rootlogger, cmd.LoglevelFlag, cmd.LogjsonFlag, cmd.SyslogFlag, cmd.LogfilenameFlag)
logging.SetupLogging(logger, cmd.LoglevelFlag, cmd.LogjsonFlag, cmd.SyslogFlag, cmd.LogfilenameFlag)
logger.Info("log rotation")
case syscall.SIGINT:
default:
logger.Info("Unsupported signal", "signal", sig)
}
}
}()
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, syscall.SIGUSR1)
logger.Debug("PIDs", "parent", os.Getpid(), "child", childProcess.Process.Pid)
state, _ := childProcess.Process.Wait()
_ = wDeadManPipe.Close()
if code, ok := state.Sys().(syscall.WaitStatus); ok {
status := code.ExitStatus()
if status == 0 {
return nil
}
return fatalError("Serve child returned a non-zero exit status", nil)
}
return nil
}
func execParent() error {
err := scomp.SetupSeccomp(-1)
if err != nil {
return fatalError("Error setting up seccomp", err)
}
err = scomp.SetupPledge(-1)
if err != nil {
return fatalError("Error setting up pledge", err)
}
name, err := parseCobraFlags()
if err != nil && err != pflag.ErrHelp {
return fatalError("Error parsing flags", err)
}
if name == "serve" && err != pflag.ErrHelp {
return execServeParent()
}
return cmd.Execute()
}
func parseCobraFlags() (string, error) {
c, _, err := cmd.RootCmd.Find(os.Args[1:])
if err != nil {
return c.Name(), err
}
var flags []string
c, flags, err = c.Find(os.Args[1:])
if err != nil {
return c.Name(), err
}
return c.Name(), c.ParseFlags(flags)
}
func exit(code int) {
memguard.DestroyAll()
os.Stdout.Close()
os.Stderr.Close()
os.Exit(code)
}
func main() {
defer func() {
e := recover()
if e == nil {
exit(0)
}
if err, ok := e.(error); ok {
if eerrors.Is("Fatal", err) {
fmt.Fprintln(os.Stderr, err.Error())
exit(1)
}
}
panic(e)
}()
err := doMain()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
exit(1)
}
exit(0)
}
var cancelLogger context.CancelFunc
var loggerCtx context.Context
func init() {
loggerCtx, cancelLogger = context.WithCancel(context.Background())
}
func runUnconfined(t base.Types) error {
name, err := base.Name(t, false)
if err != nil {
return fatalError("Unknown process name", nil)
}
sid := os.Getenv("SKEWER_SESSION")
ctx, cancel := context.WithCancel(context.Background())
signal.Ignore(syscall.SIGHUP, syscall.SIGINT)
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGTERM)
go func() {
sig := <-sigchan
if sig != nil {
cancel()
}
}()
switch t {
case base.Configuration:
dumpable.SetNonDumpable()
capabilities.NoNewPriv()
var loggerHandle uintptr = 3
var ringSecret *memguard.LockedBuffer
rPipe := os.NewFile(4, "ringsecretpipe")
buf := make([]byte, 32)
_, err := rPipe.Read(buf)
if err != nil {
return fatalError("Could not read ring secret:", err)
}
ringSecret, err = memguard.NewImmutableFromBytes(buf)
if err != nil {
return fatalError("Could not create ring secret:", err)
}
_ = rPipe.Close()
sessionID := utils.MustParseULID(sid)
ring := kring.GetRing(kring.RingCreds{Secret: ringSecret, SessionID: sessionID})
boxsecret, err := ring.GetBoxSecret()
if err != nil {
return fatalError("kring getboxsecret error", err)
}
logger, err := getLogger(loggerCtx, name, boxsecret, loggerHandle)
if err != nil {
return fatalError("Could not create a logger for the configuration service", err)
}
err = scomp.SetupSeccomp(t)
if err != nil {
return fatalError("Seccomp setup error", err)
}
err = scomp.SetupPledge(t)
if err != nil {
return fatalError("Pledge setup error", err)
}
err = services.LaunchConfProvider(ctx, ring, os.Getenv("SKEWER_CONFINED") == "TRUE", logger)
if err != nil {
return fatalError("ConfigurationProvider encountered a fatal error", err)
}
return nil
case base.TCP,
base.UDP,
base.Graylog,
base.RELP,
base.DirectRELP,
base.Journal,
base.Store,
base.Accounting,
base.MacOS,
base.KafkaSource,
base.Filesystem,
base.HTTPServer:
if t == base.Store {
runtime.GOMAXPROCS(128)
}
dumpable.SetNonDumpable()
capabilities.NoNewPriv()
var binderClient binder.Client
logger := log15.New()
var pipe *os.File
var err error
var handle uintptr = 3
var binderHdl uintptr
var loggerHdl uintptr
var pipeHdl uintptr
var ringSecretHdl uintptr
var ringSecret *memguard.LockedBuffer
if os.Getenv("SKEWER_HAS_BINDER") == "TRUE" {
binderHdl = handle
handle++
}
if os.Getenv("SKEWER_HAS_LOGGER") == "TRUE" {
loggerHdl = handle
handle++
}
if os.Getenv("SKEWER_HAS_PIPE") == "TRUE" {
pipeHdl = handle
handle++
}
ringSecretHdl = handle
rPipe := os.NewFile(ringSecretHdl, "ringsecretpipe")
buf := make([]byte, 32)
_, err = rPipe.Read(buf)
if err != nil {
return fatalError("Could not read ring secret:", err)
}
ringSecret, err = memguard.NewImmutableFromBytes(buf)
if err != nil {
return fatalError("Could not create ring secret:", err)
}
_ = rPipe.Close()
sessionID := utils.MustParseULID(sid)
ring := kring.GetRing(kring.RingCreds{Secret: ringSecret, SessionID: sessionID})
boxsecret, err := ring.GetBoxSecret()
if err != nil {
return fatalError("kring getboxsecret error", err)
}
if loggerHdl > 0 {
logger, err = getLogger(loggerCtx, name, boxsecret, loggerHdl)
if err != nil {
return fatalError("Could not create logger for plugin:", err)
}
}
if binderHdl > 0 {
binderClient, _ = binder.NewClient(os.NewFile(binderHdl, "bfile"), boxsecret, logger)
if err != nil {
return fatalError("Could not create binder for plugin:", err)
}
}
if pipeHdl > 0 {
pipe = os.NewFile(pipeHdl, "pipe")
}
err = scomp.SetupSeccomp(t)
if err != nil {
return fatalError("Seccomp setup error", err)
}
err = scomp.SetupPledge(t)
if err != nil {
return fatalError("Pledge setup error", err)
}
err = services.Launch(
ctx,
t,
services.SetConfined(os.Getenv("SKEWER_CONFINED") == "TRUE"),
services.SetProfile(os.Getenv("SKEWER_PROFILE") == "TRUE"),
services.SetRing(ring),
services.SetBinder(binderClient),
services.SetLogger(logger),
services.SetPipe(pipe),
)
if err != nil {
return fatalError("Plugin encountered a fatal error", err)
}
return nil
}
return fatalError("Unknown process name", nil)
}
func runConfined(t base.Types) error {
name, err := base.Name(t, true)
if err != nil {
return fatalError("Unknown process name", nil)
}
switch t {
case base.Journal:
// journal is a special case, as /run/log/journal and /var/log/journal
// can not be bind-mounted (probably because of setgid bits)
dumpable.SetNonDumpable()
path, err := osext.Executable()
if err != nil {
return fatalError("Error getting executable path", err)
}
// mask most of directories, but no pivotroot
err = namespaces.SetJournalFs(path)
if err != nil {
return fatalError("mount error", err)
}
err = sys.SetHostname(name)
if err != nil {
return fatalError("sethostname error", err)
}
// from here we don't need root capabilities in the container
err = capabilities.DropAllCapabilities()
if err != nil {
return fatalError("Error dropping caps", err)
}
err = syscall.Exec(path, []string{os.Args[0][9:]}, os.Environ())
if err != nil {
return fatalError("execve error", err)
}
return nil
case base.TCP,
base.UDP,
base.Graylog,
base.RELP,
base.DirectRELP,
base.Store,
base.Accounting,
base.MacOS,
base.Configuration,
base.KafkaSource,
base.Filesystem,
base.HTTPServer:
path, err := osext.Executable()
if err != nil {
return fatalError("Error getting executable path", err)
}
// we are root inside the user namespace that was created by plugin control
dumpable.SetNonDumpable()
root, err := namespaces.MakeChroot(path)
if err != nil {
return fatalError("mount error", err)
}
err = namespaces.PivotRoot(root)
if err != nil {
return fatalError("pivotroot error", err)
}
err = sys.SetHostname(name)
if err != nil {
return fatalError("sethostname error", err)
}
// from here we don't need root capabilities in the container
err = capabilities.DropAllCapabilities()
if err != nil {
return fatalError("Error dropping caps", err)
}
environ := append(os.Environ(), "SKEWER_CONFINED=TRUE")
err = syscall.Exec(path, []string{os.Args[0][9:]}, environ)
if err != nil {
return fatalError("execve error", err)
}
return nil
}
return fatalError("Unknown process name", nil)
}
func doMain() error {
defer func() {
cancelLogger()
time.Sleep(100 * time.Millisecond)
}()
// calculate the process name
name := strings.Trim(os.Args[0], "./ \n\r")
spl := strings.Split(name, "/")
if len(spl) > 0 {
name = spl[len(spl)-1]
} else {
return fatalError("Unknown process name", nil)
}
if runtime.GOOS == "openbsd" {
// so that we execute IP capabilities probes before the call to pledge
_, _ = net.Dial("udp4", "127.0.0.1:80")
}
switch name {
case "skewer-child":
if runtime.GOOS == "linux" {
return reExec(netBindCapDrop)
}
return execChild()
case "skewer-linux-child":
return execChild()
case "skewer-parent-dropped":
return execParent()
case "skewer":
if runtime.GOOS == "linux" {
err := reExec(earlyCapsDrop) // we drop most of caps on linux before we even parse the command line
if err != nil {
return err
}
err = reExec(parentCapsDrop) // now we can setuid, setgid
if err != nil {
return err
}
}
return execParent()
}
typ, cfnd, err := base.Type(name)
if err != nil {
return fatalError("Unknown process name", nil)
}
if cfnd {
return runConfined(typ)
}
return runUnconfined(typ)
}