Skip to content

Commit cd77289

Browse files
authored
Merge pull request #1855 from pendo324/guestagent-audit-edgecase
fix: guestagent audit detection and fallback
2 parents 6bc0a45 + 127df53 commit cd77289

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

pkg/guestagent/guestagent_linux.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,25 @@ func New(newTicker func() (<-chan time.Time, func()), iptablesIdle time.Duration
2727

2828
auditClient, err := libaudit.NewMulticastAuditClient(nil)
2929
switch {
30+
// syscall.EPROTONOSUPPORT or syscall.EAFNOSUPPORT is returned when calling attempting to connect to NETLINK_AUDIT
31+
// on a kernel built without auditing support.
32+
// https://github.com/elastic/go-libaudit/blob/ec298e53a6841a1f7715abbc7122635622f349bd/audit.go#L112-L115
3033
case errors.Is(err, syscall.EPROTONOSUPPORT), errors.Is(err, syscall.EAFNOSUPPORT):
31-
// system doesn't support auditing, skip
32-
a.worthCheckingIPTables = true
33-
go a.kubernetesServiceWatcher.Start()
34-
go a.fixSystemTimeSkew()
35-
return a, nil
34+
return startGuestAgentRoutines(a, false)
3635
case !errors.Is(err, nil):
3736
return nil, err
3837
}
3938

39+
// syscall.EPERM is returned when using audit from a non-initial namespace
40+
// https://github.com/torvalds/linux/blob/633b47cb009d09dc8f4ba9cdb3a0ca138809c7c7/kernel/audit.c#L1054-L1057
4041
auditStatus, err := auditClient.GetStatus()
41-
if err != nil {
42+
switch {
43+
case errors.Is(err, syscall.EPERM):
44+
return startGuestAgentRoutines(a, false)
45+
case !errors.Is(err, nil):
4246
return nil, err
4347
}
48+
4449
if auditStatus.Enabled == 0 {
4550
if err = auditClient.SetEnabled(true, libaudit.WaitForReply); err != nil {
4651
return nil, err
@@ -59,8 +64,21 @@ func New(newTicker func() (<-chan time.Time, func()), iptablesIdle time.Duration
5964
} else {
6065
a.worthCheckingIPTables = true
6166
}
67+
return startGuestAgentRoutines(a, true)
68+
}
69+
70+
// startGuestAgentRoutines sets worthCheckingIPTables to true if auditing is not supported,
71+
// instead of using setWorthCheckingIPTablesRoutine to dynamically set the value.
72+
//
73+
// Auditing is not supported in a kernels and is not currently supported outside of the initial namespace, so does not work
74+
// from inside a container or WSL2 instance, for example.
75+
func startGuestAgentRoutines(a *agent, supportsAuditing bool) (*agent, error) {
76+
if !supportsAuditing {
77+
a.worthCheckingIPTables = true
78+
}
6279
go a.kubernetesServiceWatcher.Start()
6380
go a.fixSystemTimeSkew()
81+
6482
return a, nil
6583
}
6684

0 commit comments

Comments
 (0)