Skip to content

Commit

Permalink
Merge pull request #1542 from Prateeknandle/mount
Browse files Browse the repository at this point in the history
fix(bpf) : update prepend_path for handling mount of file path
  • Loading branch information
daemon1024 authored Dec 14, 2023
2 parents bd8450f + 2f99c4e commit 15803aa
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion KubeArmor/BPF/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ static __always_inline bool prepend_path(struct path *path, bufs_t *string_p) {
m = BPF_CORE_READ(mnt, mnt_parent);
if (mnt != m) {
dentry = BPF_CORE_READ(mnt, mnt_mountpoint);
mnt = m;
mnt = BPF_CORE_READ(mnt, mnt_parent);
vfsmnt = &mnt->mnt;
continue;
}
break;
Expand Down
3 changes: 2 additions & 1 deletion KubeArmor/BPF/system_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ static __always_inline bool prepend_path(struct path *path, bufs_t *string_p, in
if (mnt != m)
{
bpf_probe_read(&dentry, sizeof(struct dentry *), &mnt->mnt_mountpoint);
mnt = m;
bpf_probe_read(&mnt, sizeof(struct mount *), &mnt->mnt_parent);
vfsmnt = &mnt->mnt;
continue;
}

Expand Down
Binary file modified KubeArmor/enforcer/bpflsm/enforcer_bpfeb.o
Binary file not shown.
Binary file modified KubeArmor/enforcer/bpflsm/enforcer_bpfel.o
Binary file not shown.
Binary file modified KubeArmor/enforcer/bpflsm/enforcer_path_bpfeb.o
Binary file not shown.
Binary file modified KubeArmor/enforcer/bpflsm/enforcer_path_bpfel.o
Binary file not shown.
Binary file modified KubeArmor/utils/bpflsmprobe/probe_bpfeb.o
Binary file not shown.
Binary file modified KubeArmor/utils/bpflsmprobe/probe_bpfel.o
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/k8s_env/block/res/ksp-wordpress-allow-file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ spec:
- dir: /bin/
- dir: /pts/
recursive: true
- dir: /dev/
recursive: true
matchPaths:
- path: /root/.bashrc
- path: /root/.bash_history
Expand Down
2 changes: 2 additions & 0 deletions tests/k8s_env/configmap/manifests/ksp-unannotated-allow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ spec:
- dir: /usr/bin/
- dir: /proc/
recursive: true
- dir: /dev/
recursive: true
matchPaths:
- path: /dev/tty
- path: /lib/terminfo/x/xterm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ spec:
recursive: true
- dir: /proc/
recursive: true
- dir: /dev/
recursive: true
- dir: /lib/x86_64-linux-gnu/
- dir: /bin/
action:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ spec:
recursive: true
- dir: /proc/
recursive: true
- dir: /dev/
recursive: true
- dir: /lib/x86_64-linux-gnu/
- dir: /bin/
# - dir: /etc/ # required to change root to user1 (coarse-grained way)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ spec:
recursive: true
- dir: /bin/
recursive: true
- dir: /dev/
recursive: true
action:
Allow

Expand Down
17 changes: 17 additions & 0 deletions tests/k8s_env/smoke/res/ksp-wordpress-block-mount-file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: security.kubearmor.com/v1
kind: KubeArmorPolicy
metadata:
name: ksp-wordpress-block-mount-file
namespace: wordpress-mysql
spec:
severity: 5
selector:
matchLabels:
app: wordpress
file:
matchDirectories:
- dir: /dev/shm/
readOnly: true
recursive: true
action:
Block
25 changes: 25 additions & 0 deletions tests/k8s_env/smoke/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,31 @@ var _ = Describe("Smoke", func() {
fmt.Printf("OUTPUT: %s\n", sout)
Expect(sout).To(MatchRegexp("/etc/shadow.*Permission denied"))
})

It("can block write access and only allow read access to mounted files", func() {
// Apply policy
err := K8sApplyFile("res/ksp-wordpress-block-mount-file.yaml")
Expect(err).To(BeNil())

// Start Kubearmor Logs
err = KarmorLogStart("policy", "wordpress-mysql", "File", wp)
Expect(err).To(BeNil())

// wait for policy creation
time.Sleep(5 * time.Second)

sout, _, err := K8sExecInPod(wp, "wordpress-mysql",
[]string{"bash", "-c", "touch /dev/shm/new"})
Expect(err).To(BeNil())
fmt.Printf("OUTPUT: %s\n", sout)
Expect(sout).To(ContainSubstring("Permission denied"))

// check policy violation alert
_, alerts, err := KarmorGetLogs(5*time.Second, 1)
Expect(err).To(BeNil())
Expect(alerts[0].PolicyName).To(Equal("ksp-wordpress-block-mount-file"))
Expect(alerts[0].Severity).To(Equal("5"))
})
})

})

0 comments on commit 15803aa

Please sign in to comment.