Skip to content

Commit

Permalink
Handle readonly File violation corner case for Audit mode
Browse files Browse the repository at this point in the history
This commit adds new field ReadOnly to MatchPolicy struct.

Fixes: kubearmor#213

Signed-off-by: Ayush Dwivedi <ayush.dwivedi@accuknox.com>
  • Loading branch information
oneiro-naut committed Jul 28, 2021
1 parent 490bb56 commit 2f58fdd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
17 changes: 17 additions & 0 deletions KubeArmor/feeder/policyMatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (fd *Feeder) newMatchPolicy(policyEnabled int, policyName, src string, mp i
match.Resource = fpt.Path
match.ResourceType = "Path"

match.ReadOnly = fpt.ReadOnly

if policyEnabled == tp.KubeArmorPolicyAudited && strings.HasPrefix(fpt.Action, "Block") {
match.Action = "Audit (" + fpt.Action + ")"
} else {
Expand All @@ -113,6 +115,8 @@ func (fd *Feeder) newMatchPolicy(policyEnabled int, policyName, src string, mp i
match.Resource = fdt.Directory
match.ResourceType = "Directory"

match.ReadOnly = fdt.ReadOnly

if policyEnabled == tp.KubeArmorPolicyAudited && strings.HasPrefix(fdt.Action, "Block") {
match.Action = "Audit (" + fdt.Action + ")"
} else {
Expand All @@ -126,6 +130,8 @@ func (fd *Feeder) newMatchPolicy(policyEnabled int, policyName, src string, mp i
match.Resource = fpt.Pattern
match.ResourceType = "" // to be defined based on the pattern matching syntax

match.ReadOnly = fpt.ReadOnly

if policyEnabled == tp.KubeArmorPolicyAudited && strings.HasPrefix(fpt.Action, "Block") {
match.Action = "Audit (" + fpt.Action + ")"
} else {
Expand Down Expand Up @@ -791,6 +797,12 @@ func (fd *Feeder) UpdateMatchedPolicy(log tp.Log) tp.Log {

log.Type = "MatchedPolicy"
log.Action = secPolicy.Action

if log.Operation == "File" {
if secPolicy.ReadOnly && log.Data != "" && (strings.Contains(log.Data, "O_RDWR") || strings.Contains(log.Data, "O_WRONLY")) {
log.Action = "Block"
}
}
}
}
}
Expand Down Expand Up @@ -918,6 +930,11 @@ func (fd *Feeder) UpdateMatchedPolicy(log tp.Log) tp.Log {
// return tp.Log{}
// }

if log.PolicyEnabled == tp.KubeArmorPolicyAudited && log.Action == "Block" {
log.Action = "Audit (Block)"
return log
}

return log
}
} else { // host
Expand Down
5 changes: 3 additions & 2 deletions KubeArmor/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ type MatchPolicy struct {
Resource string
ResourceType string

Regexp *regexp.Regexp
Native bool
Regexp *regexp.Regexp
Native bool
ReadOnly bool

Action string
}
Expand Down
2 changes: 1 addition & 1 deletion tests/scenarios/multiubuntu_test_9/cmd2
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ result: failed
---
operation: File
condition: password
action: Allow
action: Block

0 comments on commit 2f58fdd

Please sign in to comment.