forked from elastic/go-libaudit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit_test.go
887 lines (754 loc) · 21.9 KB
/
audit_test.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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// +build linux
package libaudit
import (
"encoding/base64"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"io"
"os"
"runtime"
"syscall"
"testing"
"time"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/elastic/go-libaudit/v2/rule"
"github.com/elastic/go-libaudit/v2/rule/flags"
"github.com/elastic/go-libaudit/v2/sys"
)
// This can be run inside of Docker with:
// docker run -it --rm -v `pwd`:/go/src/github.com/elastic/go-libaudit --pid=host --privileged golang:1.10.2 /bin/bash
var (
hexdump = flag.Bool("hexdump", false, "dump kernel responses to stdout in hexdump -C format")
list = flag.Bool("l", false, "dump rules")
immutable = flag.Bool("immutable", false, "test setting kernel as immutable (requires reboot to undo)")
)
// testRule is a base64 representation of the following rule.
// -a always,exit -S open,truncate -F dir=/etc -F success=0
const testRule = `BAAAAAIAAAACAAAABAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGsAAABoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAvZXRj`
func TestAuditClientGetStatus(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to get audit status")
}
status, err := getStatus(t)
if err != nil {
t.Fatal(err)
}
t.Logf("Status: %+v", status)
}
func TestAuditClientGetStatusPermissionError(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("must be non-root to test permission failure")
}
status, err := getStatus(t)
assert.Nil(t, status, "status should be nil")
// ECONNREFUSED means we are in a username space.
// EPERM means we are not root.
if err != syscall.ECONNREFUSED && err != syscall.EPERM {
t.Fatal("unexpected error")
}
}
func getStatus(t testing.TB) (*AuditStatus, error) {
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
return c.GetStatus()
}
func TestDeleteRules(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to get audit status")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
n, err := c.DeleteRules()
if err != nil {
t.Fatal(err)
}
t.Logf("%v rules deleted", n)
}
func TestListRules(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to list rules")
}
if !*list {
t.SkipNow()
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
rules, err := c.GetRules()
if err != nil {
t.Fatal(err)
}
for i, rule := range rules {
b64 := base64.StdEncoding.EncodeToString(rule)
t.Logf("rule %v - (base64):\n%v", i, b64)
t.Logf("rule %v - (hexdump):\n%v", i, hex.Dump(rule))
}
}
func TestAddRule(t *testing.T) {
if sys.GetEndian() != binary.LittleEndian {
t.Skip("testRule is for little endian, but test machine is big endian")
}
if os.Geteuid() != 0 {
t.Skip("must be root to get audit status")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.DeleteRules()
rawRule, _ := base64.StdEncoding.DecodeString(testRule)
if err := c.AddRule(rawRule); err != nil {
t.Fatal(err)
}
t.Log("rule added")
}
func TestAddDuplicateRule(t *testing.T) {
if sys.GetEndian() != binary.LittleEndian {
t.Skip("testRule is for little endian, but test machine is big endian")
}
if os.Geteuid() != 0 {
t.Skip("must be root to get audit status")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.DeleteRules()
// Add first rule.
rawRule, _ := base64.StdEncoding.DecodeString(testRule)
if err := c.AddRule(rawRule); err != nil {
t.Fatal(err)
}
// Add duplicate rule.
err = c.AddRule(rawRule)
if err == nil {
t.Fatal("expected error about duplicate rule")
}
assert.Contains(t, err.Error(), "rule exists")
}
func TestAuditClientGetRules(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to get audit status")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
rules, err := c.GetRules()
if err != nil {
t.Fatalf("%+v", err)
}
for _, r := range rules {
fmt.Printf("%+v\n", r)
}
}
func TestAuditClientSetPID(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to set audit pid")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
err = c.SetPID(WaitForReply)
if err != nil {
t.Fatal(err)
}
t.Log("SetPID complete")
}
func TestAuditClientSetEnabled(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to enable audit")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
err = c.SetEnabled(true, WaitForReply)
if err != nil {
t.Fatal(err)
}
t.Log("SetEnabled complete")
status, err := getStatus(t)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, 1, status.Enabled)
}
func TestAuditClientSetFailure(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to enable audit")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
err = c.SetFailure(LogOnFailure, WaitForReply)
if err != nil {
t.Fatal(err)
}
t.Log("SetFailure complete")
status, err := getStatus(t)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, LogOnFailure, status.Failure)
err = c.SetFailure(SilentOnFailure, WaitForReply)
if err != nil {
t.Fatal(err)
}
t.Log("SetFailure complete")
status, err = getStatus(t)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, SilentOnFailure, status.Failure)
}
func TestAuditClientSetRateLimit(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to set rate limit")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
var limit uint32 = 1233
err = c.SetRateLimit(limit, WaitForReply)
if err != nil {
t.Fatal(err)
}
t.Log("SetRateLimit complete")
status, err := getStatus(t)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, limit, status.RateLimit)
}
func TestAuditClientSetBacklogLimit(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to set rate limit")
}
status, err := getStatus(t)
if err != nil {
t.Fatal(err)
}
if status.FeatureBitmap&AuditFeatureBitmapBacklogLimit == 0 {
t.Skip("backlog limit feature not supported in current kernel")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
var limit uint32 = 10002
err = c.SetBacklogLimit(limit, WaitForReply)
if err != nil {
t.Fatal(err)
}
t.Log("SetBacklogLimit complete")
status, err = getStatus(t)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, limit, status.BacklogLimit)
}
func TestMulticastAuditClient(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to bind to netlink audit socket")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
// Start the testing.
client, err := NewMulticastAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer client.Close()
// Receive (likely no messages will be received).
var msgCount int
for i := 0; i < 5; i++ {
msg, err := client.Receive(true)
if err == syscall.EAGAIN {
time.Sleep(500 * time.Millisecond)
continue
} else if err != nil {
t.Fatal(err)
} else {
t.Logf("Received: type=%v, msg=%v", msg.Type, string(msg.Data))
msgCount++
}
}
t.Logf("received %d messages", msgCount)
}
func TestAuditClientReceive(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to set audit port id")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
// Open two clients from this process to consume the port ID that's
// equal to the process ID. The next client will have a random port ID
// and this will test that the client works properly when two sockets are
// used.
observer, err := NewAuditClient(nil)
if err != nil {
t.Fatal(err)
}
defer observer.Close()
if err = observer.SetEnabled(false, WaitForReply); err != nil {
t.Fatal("failed to disable audit", err)
}
defer func() {
status, err := observer.GetStatus()
t.Logf("get status: status=%+v, err=%v", status, err)
}()
// Start the testing.
client, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer client.Close()
status, err := client.GetStatus()
if err != nil {
t.Fatal("get status failed", err)
}
t.Logf("status=%+v, process_id=%v", status, os.Getpid())
err = client.SetEnabled(true, WaitForReply)
if err != nil {
t.Fatal("set enabled failed:", err)
}
err = client.SetBacklogLimit(1024, WaitForReply)
if err != nil {
t.Fatal("set backlog limit failed:", err)
}
err = client.SetPID(WaitForReply)
if err != nil {
t.Fatal("set pid failed:", err, " (Did you stop auditd?)")
}
kMajor, kMinor, err := kernelVersion()
if err != nil {
t.Fatal("get kernel version failed:", err)
}
// Setting a new PID when an audit process is already registered only fails
// in Linux kernel version 4.11 and up.
if kMajor > 4 || kMajor == 4 && kMinor >= 11 {
// Depending on the kernel version, it will reply with an AUDIT_REPLACE (1329)
// message, followed by an AUDIT_CONFIG_CHANGE (1305) message, followed
// by an ACK. Older kernels seem to not send the AUDIT_CONFIG_CHANGE message.
if err = client.SetPID(WaitForReply); err == nil {
t.Fatal("set pid (overwrite) didn't fail as expected.")
} else if errors.Cause(err) != syscall.EEXIST {
t.Fatal("expected second SetPID call to result in EEXISTS but got", err)
}
}
// Expect at least 1 message caused by our previous call (CONFIG_CHANGE).
var msgCount int
for i := 0; i < 10; i++ {
msg, err := client.Receive(true)
if err == syscall.EAGAIN {
time.Sleep(500 * time.Millisecond)
continue
} else if err != nil {
t.Fatal(err)
} else {
t.Logf("Received: type=%v, msg=%v", msg.Type, string(msg.Data))
msgCount++
}
}
assert.True(t, msgCount >= 1, "expected at least one messages")
}
func TestAuditClientClose(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to set audit port id")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
// Open a secondary client to verify that audit PID value.
observer, err := NewAuditClient(nil)
if err != nil {
t.Fatal(err)
}
defer observer.Close()
// Start the testing.
client, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer client.Close()
err = client.SetPID(WaitForReply)
if err != nil {
t.Fatal("set pid failed:", err, " (Did you stop auditd?)")
}
// Validate the PID.
status, err := observer.GetStatus()
if err != nil {
t.Fatal("failed to get audit status", err)
}
assert.NotZero(t, status.PID)
// Close will clear the PID to 0.
if err := client.Close(); err != nil {
t.Fatal(err)
}
// Validate the PID is cleared.
status, err = observer.GetStatus()
if err != nil {
t.Fatal("failed to get audit status", err)
}
assert.EqualValues(t, 0, status.PID)
}
func TestAuditStatusMask(t *testing.T) {
assert.EqualValues(t, 0x0001, AuditStatusEnabled)
assert.EqualValues(t, 0x0002, AuditStatusFailure)
assert.EqualValues(t, 0x0004, AuditStatusPID)
assert.EqualValues(t, 0x0008, AuditStatusRateLimit)
assert.EqualValues(t, 0x00010, AuditStatusBacklogLimit)
assert.EqualValues(t, 0x00020, AuditStatusBacklogWaitTime)
}
func TestAuditWaitForPendingACKs(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to change settings")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
// Perform some asynchronous requests
var limit uint32 = 10002
if err = c.SetBacklogLimit(limit, NoWait); err != nil {
t.Fatal(err, "set backlog limit failed:", err)
}
failureMode := PanicOnFailure
if err = c.SetFailure(failureMode, NoWait); err != nil {
t.Fatal(err, "set failure mode failed:", err)
}
// Wait for completion
if err = c.WaitForPendingACKs(); err != nil {
t.Fatal(err, "wait for pending ACKs failed:", err)
}
// Perform synchronous request
if err = c.SetPID(WaitForReply); err != nil {
t.Fatal("set pid failed:", err, " (Did you stop auditd?)")
}
t.Log("WaitForPendingACKs complete")
}
func TestAuditClientSetBacklogWaitTime(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to set backlog wait time")
}
status, err := getStatus(t)
if err != nil || status == nil {
t.Skipf("audit status not available: %v", err)
}
if status.FeatureBitmap&AuditFeatureBitmapBacklogWaitTime == 0 {
t.Skip("backlog wait time feature not supported in current kernel")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
for _, waitValue := range []int32{0, 500} {
msg := fmt.Sprintf("[wait time = %d]", waitValue)
err = c.SetBacklogWaitTime(waitValue, WaitForReply)
if err != nil {
t.Fatal(err, msg)
}
t.Log("SetBacklogWaitTime complete", msg)
status, err := getStatus(t)
if err != nil {
t.Fatal(err, msg)
}
assert.EqualValues(t, waitValue, status.BacklogWaitTime, msg)
}
}
func TestAuditFeatureBitmap(t *testing.T) {
assert.EqualValues(t, 0x01, AuditFeatureBitmapBacklogLimit)
assert.EqualValues(t, 0x02, AuditFeatureBitmapBacklogWaitTime)
assert.EqualValues(t, 0x04, AuditFeatureBitmapExecutablePath)
assert.EqualValues(t, 0x08, AuditFeatureBitmapExcludeExtend)
assert.EqualValues(t, 0x10, AuditFeatureBitmapSessionIDFilter)
assert.EqualValues(t, 0x20, AuditFeatureBitmapLostReset)
}
func TestAuditClientGetStatusAsync(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to get audit status")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
for _, withACK := range []bool{true, false} {
seq, err := c.GetStatusAsync(withACK)
if err != nil {
t.Fatal(err)
}
if withACK {
// Get the ack message which is a NLMSG_ERROR type whose error code is SUCCESS.
ack, err := c.getReply(seq)
if err != nil {
t.Fatal(err)
}
if ack.Header.Type != syscall.NLMSG_ERROR {
t.Fatalf("unexpected ACK to GET, got type=%d", ack.Header.Type)
}
if err = ParseNetlinkError(ack.Data); err != nil {
t.Fatal(err)
}
}
reply, err := c.getReply(seq)
if err != nil {
t.Fatal(err)
}
if reply.Header.Type != AuditGet {
t.Fatalf("unexpected reply to GET, got type=%d", reply.Header.Type)
}
replyStatus := &AuditStatus{}
if err := replyStatus.FromWireFormat(reply.Data); err != nil {
t.Fatal(err)
}
t.Logf("Status: %+v", replyStatus)
}
}
func TestAuditClientSetImmutable(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("must be root to enable audit")
}
if !*immutable {
t.Skip("use -immutable to test SetImmutable")
}
var dumper io.WriteCloser
if *hexdump {
dumper = hex.Dumper(os.Stdout)
defer dumper.Close()
}
c, err := NewAuditClient(dumper)
if err != nil {
t.Fatal(err)
}
defer c.Close()
err = c.SetImmutable(WaitForReply)
if err != nil {
t.Fatal(err)
}
t.Log("SetImmutable complete")
status, err := getStatus(t)
if err != nil {
t.Fatal(err)
}
assert.EqualValues(t, 2, status.Enabled)
}
func TestRuleParsing(t *testing.T) {
var rules []string
switch runtime.GOARCH {
case "386":
rules = []string{
"-a always,exit -F arch=b32 -S execve,execveat -F key=exec",
"-a never,exit -F arch=b32 -S bind,connect,accept4 -F key=external-access",
"-w /etc/group -p wa",
"-w /etc/passwd -p rx",
"-w /etc/gshadow -p rwxa",
"-w /tmp/test -p rwa",
"-a always,exit -F arch=b32 -S open,creat,truncate,ftruncate,openat,open_by_handle_at -F exit=-EACCES -F key=access",
"-a never,exit -F arch=b32 -S open,creat,truncate,ftruncate,openat,open_by_handle_at -F exit=-EPERM -F key=access",
"-a always,exit -F arch=b32 -S open -F key=admin -F uid=root -F gid=root -F exit=33 -F path=/tmp -F perm=rwxa",
"-a always,exit -F arch=b32 -S open -F key=key -F uid=30000 -F gid=333 -F exit=-151111 -F filetype=fifo",
"-a never,exclude -F msgtype=GRP_CHAUTHTOK",
"-a always,user -F uid=root",
"-a always,task -F uid=root",
"-a always,exit -S mount -F pid=1234",
}
case "amd64":
rules = []string{
"-a always,exit -F arch=b64 -S execve,execveat -F key=exec",
"-a never,exit -F arch=b64 -S connect,accept,bind -F key=external-access",
"-w /etc/group -p wa",
"-w /etc/passwd -p rx",
"-w /etc/gshadow -p rwxa",
"-w /tmp/test -p rwa",
"-a always,exit -F arch=b64 -S open,truncate,ftruncate,creat,openat,open_by_handle_at -F exit=-EACCES -F key=access",
"-a never,exit -F arch=b64 -S open,truncate,ftruncate,creat,openat,open_by_handle_at -F exit=-EPERM -F key=access",
"-a always,exit -F arch=b32 -S open -F key=admin -F uid=root -F gid=root -F exit=33 -F path=/tmp -F perm=rwxa",
"-a always,exit -F arch=b64 -S open -F key=key -F uid=30000 -F gid=333 -F exit=-151111 -F filetype=fifo",
"-a never,exclude -F msgtype=GRP_CHAUTHTOK",
"-a always,user -F uid=root",
"-a always,task -F uid=root",
"-a always,exit -S mount -F pid=1234",
}
default:
// Can't have multiple syscall testing as ordering of individual syscalls
// will vary between platforms (sorted by syscall id)
rules = []string{
"-a always,exit -S execve -F key=exec",
"-w /etc/group -p wa",
"-w /etc/passwd -p rx",
"-w /etc/gshadow -p rwxa",
"-w /tmp/test -p rwa",
"-a always,exit -S all -F exit=-EACCES -F key=access",
"-a never,exit -S all -F exit=-EPERM -F key=access",
"-a always,exit -S open -F key=admin -F uid=root -F gid=root -F exit=33 -F path=/tmp -F perm=rwxa",
"-a always,exit -S open -F key=key -F uid=30000 -F gid=333 -F exit=-151111 -F filetype=fifo",
"-a never,exclude -F msgtype=GRP_CHAUTHTOK",
"-a always,user -F uid=root",
"-a always,task -F uid=root",
"-a always,exit -S mount -F pid=1234",
}
}
t.Logf("checking %d rules", len(rules))
for idx, line := range rules {
msg := fmt.Sprintf("parsing line #%d: `%s`", idx, line)
r, err := flags.Parse(line)
if err != nil {
t.Fatal(err, msg)
}
data, err := rule.Build(r)
if err != nil {
t.Fatal(err, msg)
}
cmdline, err := rule.ToCommandLine(data, true)
if err != nil {
t.Fatal(err, msg)
}
assert.Equal(t, line, cmdline, msg)
}
}
func extractDecimalNumber(s []int8, pos int) (value int, nextPos int) {
const aZero, aNine int8 = 0x30, 0x39
for value = 0; ; pos++ {
c := s[pos]
if c >= aZero && c <= aNine {
value = value*10 + int(c-aZero)
} else {
return value, pos + 1
}
}
}
func kernelVersion() (major int, minor int, err error) {
var info syscall.Utsname
if err = syscall.Uname(&info); err != nil {
return 0, 0, err
}
s := info.Release[:]
// As all major non-Intel architecture return []uint8 instead of []int8 for Utsname.Release following conversion is required
temp := make([]int8, len(s))
for index, num := range s {
temp[index] = int8(num)
}
major, pos := extractDecimalNumber(temp, 0)
minor, _ = extractDecimalNumber(temp, pos)
return major, minor, nil
}