Skip to content

Commit db3868d

Browse files
andrewkrohadriansr
authored andcommitted
Unregister PID from kernel on client close
If SetPID was used then unregister our PID for a clean exit.
1 parent 7ce01a4 commit db3868d

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Added
88

99
- Added WaitForPendingACKs to receive pending ACK messages from the kernel. #14
10+
- The AuditClient will unregister with the kernel if `SetPID` has been called. #19
1011

1112
### Changed
1213

audit.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ const (
7575
// AuditClient is a client for communicating with the Linux kernels audit
7676
// interface over netlink.
7777
type AuditClient struct {
78-
Netlink NetlinkSendReceiver
79-
pendingAcks []uint32
78+
Netlink NetlinkSendReceiver
79+
pendingAcks []uint32
80+
clearPIDOnClose bool
8081
}
8182

8283
// NewMulticastAuditClient creates a new AuditClient that binds to the multicast
@@ -292,6 +293,7 @@ func (c *AuditClient) SetPID(wm WaitMode) error {
292293
Mask: AuditStatusPID,
293294
PID: uint32(os.Getpid()),
294295
}
296+
c.clearPIDOnClose = true
295297
return c.set(status, wm)
296298
}
297299

@@ -370,6 +372,13 @@ func (c *AuditClient) Receive(nonBlocking bool) (*RawAuditMessage, error) {
370372

371373
// Close closes the AuditClient and frees any associated resources.
372374
func (c *AuditClient) Close() error {
375+
// Unregister from the kernel for a clean exit.
376+
status := AuditStatus{
377+
Mask: AuditStatusPID,
378+
PID: 0,
379+
}
380+
c.set(status, NoWait)
381+
373382
return c.Netlink.Close()
374383
}
375384

audit_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package libaudit
1818

1919
import (
20+
"encoding/base64"
2021
"encoding/hex"
2122
"flag"
2223
"fmt"
@@ -26,8 +27,7 @@ import (
2627
"testing"
2728
"time"
2829

29-
"encoding/base64"
30-
30+
"github.com/pkg/errors"
3131
"github.com/stretchr/testify/assert"
3232
)
3333

@@ -483,12 +483,13 @@ func TestAuditClientReceive(t *testing.T) {
483483
// Depending on the kernel version, it will reply with an AUDIT_REPLACE (1329)
484484
// message, followed by an AUDIT_CONFIG_CHANGE (1305) message, followed
485485
// by an ACK. Older kernels seem to not send the AUDIT_CONFIG_CHANGE message.
486-
err = client.SetPID(NoWait)
487-
if err != nil {
486+
if err = client.SetPID(WaitForReply); err == nil {
488487
t.Fatal("set pid failed:", err)
488+
} else if errors.Cause(err) != syscall.EEXIST {
489+
t.Fatal("expected second SetPID call to result in EEXISTS but got", err)
489490
}
490491

491-
// Expect at least 2 messages caused by our previous call.
492+
// Expect at least 1 message caused by our previous call (CONFIG_CHANGE).
492493
var msgCount int
493494
for i := 0; i < 10; i++ {
494495
msg, err := client.Receive(true)
@@ -502,7 +503,7 @@ func TestAuditClientReceive(t *testing.T) {
502503
msgCount++
503504
}
504505
}
505-
assert.True(t, msgCount >= 2, "expected at least two messages")
506+
assert.True(t, msgCount >= 1, "expected at least one messages")
506507
}
507508

508509
func TestAuditStatusMask(t *testing.T) {

0 commit comments

Comments
 (0)