forked from elastic/go-libaudit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit_test.go
278 lines (233 loc) · 5.99 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
// Copyright 2017 Elasticsearch Inc.
//
// Licensed 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/hex"
"flag"
"io"
"os"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var hexdump = flag.Bool("hexdump", false, "dump kernel responses to stdout in hexdump -C format")
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")
assert.Equal(t, syscall.EPERM, err)
}
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 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 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")
}
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 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?)")
}
// 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.
err = client.SetPID(NoWait)
if err != nil {
t.Fatal("set pid failed:", err)
}
// Expect at least 2 messages caused by our previous call.
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.MessageType, string(msg.RawData))
msgCount++
}
}
assert.True(t, msgCount >= 2, "expected at least two messages")
}
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)
}