Git Product home page Git Product logo

fanotify-mon's Introduction

fanotify PoC

This PoC aims to take a container rootfs and only allows access to files in it if they're signed with a particular public key.

For now it prints hashes of files accessed

Usage

sudo ./fanotify-poc ROOTFS_PATH

Caveats

Fanotify doesn't work across mount namespaces so this only works for files accessed from outside the container.

Testing go binary

  • Build the binary from this code: make build.
  • Run the binary as root sudo ./fanotify-mon --hostname="yourhost" --runtime=docker --kubeconfig="kubeconfig path"
  • Now start pods so that this application will start monitoring:
kubectl run --image nginx -l enforce.k8s.io=deny-third-party-execution nginx
  • Once the pod starts exec into it and test the following:
touch newfile
ls

rm -rf /usr/bin/touch
cat <<EOF > /usr/bin/touch
#!/bin/bash

echo this is a new touch
EOF

chmod +x /usr/bin/touch
touch file
  • The last execution of touch should be blocked and you should see error: Operation not permitted. Also the running ./fanotify-mon will show you what was denied in its logs.
  • You can see logs of the containerd process also using sudo journalctl -fu containerd.

fanotify-mon's People

Contributors

iaguis avatar knrt10 avatar surajssd avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fanotify-mon's Issues

Create Kubernetes configs to run the application

This application is supposed to run as Daemonset on Kubernetes. There already is a Dockerfile, now we need to make it run from a Kubernetes pod.

Things to consider:

  • We will need access to the containerd socket: /run/containerd/containerd.sock
  • I am not sure if we need access to the docker socket.
  • Add support to switch between using Kubeconfig and incluster config, depending on what is available.
  • This pod needs privileged permissions to run!
  • On the k8s API side this app needs permission to list all the pods on the current node. We can reuse the kubelet kubeconfig, but that will be forgery! Also it won't work long-term because going forward when we add CRDs we will need extensive RBAC policies.

Add support for policy language configmap

Right now instead of going into implementing full-fledged CRDs, rely on a configmap for the policy information. The app will read the policy from a configmap with name fanotify-policy in fanotify-mon namespace with key policy. This policy will be parsed by the application to decide what pods should be selected for policy enforcement.

Here is the document to flesh out the policy language.

Goroutine leaks on each monitored container

Every time we start monitoring a container a new goroutine is spawned but that remains active even when the container has stopped and the fanotify FD is closed.

The goroutine handleEvent, which is doing a blocking call to binary.Read via fanotify lib method GetEvent. While making a call to this function an io.Reader is passed generated from the the file descriptor.

rd := bufio.NewReader(os.NewFile(uintptr(fd), ""))

But since this call is blocked on reading from this file descriptor that we received from the kernel, even if this file descriptor is closed by us this call never returns. This is currently causing goroutine leaks.

10 @ 0xcd4925 0xcd258d 0xd3a545 0xd3a52d 0xd3a2e5 0xd436fe 0xd436f6 0xdd1af4 0xcebd3a 0xd25e27 0xd25e28 0x1fa4e85 0x1fac26a 0x1fad049 0xcbeb41
#       0xcd4924        syscall.Syscall+0x4                                                     /usr/local/go/src/syscall/asm_linux_amd64.s:20
#       0xcd258c        syscall.read+0x4c                                                       /usr/local/go/src/syscall/zsyscall_linux_amd64.go:687
#       0xd3a544        syscall.Read+0x284                                                      /usr/local/go/src/syscall/syscall_unix.go:189
#       0xd3a52c        internal/poll.ignoringEINTRIO+0x26c                                     /usr/local/go/src/internal/poll/fd_unix.go:582
#       0xd3a2e4        internal/poll.(*FD).Read+0x24                                           /usr/local/go/src/internal/poll/fd_unix.go:163
#       0xd436fd        os.(*File).read+0x5d                                                    /usr/local/go/src/os/file_posix.go:32
#       0xd436f5        os.(*File).Read+0x55                                                    /usr/local/go/src/os/file.go:119
#       0xdd1af3        bufio.(*Reader).Read+0x1b3                                              /usr/local/go/src/bufio/bufio.go:227
#       0xcebd39        io.ReadAtLeast+0x99                                                     /usr/local/go/src/io/io.go:328
#       0xd25e26        io.ReadFull+0xc46                                                       /usr/local/go/src/io/io.go:347
#       0xd25e27        encoding/binary.Read+0xc47                                              /usr/local/go/src/encoding/binary/binary.go:256
#       0x1fa4e84       github.com/s3rj1k/go-fanotify/fanotify.(*NotifyFD).GetEvent+0x64        /home/user/go/pkg/mod/github.com/s3rj1k/go-fanotify/[email protected]/fanotify.go:194
#       0x1fac269       main.(*ContainerNotifier).handleEvent+0xe9                              /home/user/code/work/fanotify-experiments/main.go:254
#       0x1fad048       main.watchContainerFANotifyEvents+0x28                                  /home/user/code/work/fanotify-experiments/main.go:368

NOTE: Got this information by adding the following snippet to the main function:

// Debug go routines
go func() {
        log.Println(http.ListenAndServe("192.168.122.252:6060", nil))
}()

Then visit curl 192.168.122.252:6060/debug/pprof/goroutine?debug=1.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.