Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime,agent: Add AppArmor support on the guest side #7587

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ManaSugi
Copy link
Member

@ManaSugi ManaSugi commented Aug 8, 2023

Kata containers support AppArmor for containers running inside the guest in order to improve security.

Containers inside the guest are launced with the Kata default profile (kata-default) applied if disable_guest_apparmor is set to false in the runtime configuration. The kata-default based on the containerd's default profile is applied to the container process inside the guest by default. Users can also set a custom AppArmor profile to the container process using guest_apparmor_profile in the runtime configuration. This will be an alternative configuration of the Kubernetes' annotation, container.apparmor.security.beta.kubernetes.io, for AppArmor because Kata users cannot apply AppArmor profiles to the guest container via Kubernetes' configuration. To apply the profile to the container, the guest rootfs must be Ubuntu or Debian that is created with APPARMOR=yes. The kata-default is stored under /etc/apparmor.d in the rootfs. This works only if the init service is systemd, not the agent init.

Fixes: #7586

Signed-off-by: Manabu Sugimoto Manabu.Sugimoto@sony.com

@ManaSugi ManaSugi requested a review from a team as a code owner August 8, 2023 10:39
@katacontainersbot katacontainersbot added the size/large Task of significant size label Aug 8, 2023
@ManaSugi
Copy link
Member Author

ManaSugi commented Aug 8, 2023

Experiments with containerd

Requirements

  • Enable the guest AppArmor in the runtime configuration
sudo sed -i '/^disable_guest_apparmor/ s/true/false/g' /etc/kata-containers/configuration.toml

Or enable it using the pod-level annotation in your sandbox.json

"annotations": {
    "io.katacontainers.config.hypervisor.disable_guest_apparmor": "false"
 }
  • Create and build a rootfs for AppArmor

The apparmor package is installed and the default profile (kata-default) is stored in the guest rootfs
if the APPARMOR is set to yes. The kata-default profile is copied from tools/osbuilder/rootfs-builder/template/apparmor/kata-default to /etc/apparmor.d in the guest rootfs.

$ script -fec 'sudo -E GOPATH=$GOPATH USE_DOCKER=true APPARMOR=yes ./rootfs.sh ubuntu'
$ script -fec 'sudo -E USE_DOCKER=true ./image_builder.sh ${ROOTFS_DIR}'

Qemu or CloudHypervisor

Kata containers with qemu or cloudhypervisor via crictl can be launched.

1. Launch Kata containers with AppArmor (default profile kata-default)

The default profile kata-default is loaded and applied to container processes by default.

Launch a container using crictl

container.json
{
    "metadata": {
        "name": "kata-ubuntu"
    },
    "image": {
        "image": "docker.io/ubuntu"
    },
    "command": [
        "/bin/sh"
    ],
    "envs": [
        {
            "key": "PATH",
            "value": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        },
        {
            "key": "TERM",
            "value": "xterm"
        }
    ],
    "stdin": true,
    "stdin_once": true,
    "tty": true,
    "linux": {
    }
}
sandbox.json
{
    "metadata": {
        "name": "kata-sandbox",
        "namespace": "default",
        "uid": "kata-sandbox"
    },
    "linux": {
    }
}
$ uname -a
Linux *** 5.10.0-1057-oem #61-Ubuntu SMP Thu Jan 13 15:06:11 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

$ sudo crictl run -r kata container.json sandbox.json
d85f905ed2aebe5ea60dcdea65d06473b42f384ee8f018b20aa5f5d4d878be83

Attach the Kata VM

You can confirm that the kata-default profile is loaded and applied to /pause and /usr/bin/dash processes.

$ sudo kata-runtime exec e0c15b199093de09720eb0f7c4954ee27ba8737039bb92620e1f74e8a255a7e9

root@localhost:/# uname -a
Linux localhost 6.1.38 #1 SMP Fri Aug  4 10:19:09 JST 2023 x86_64 x86_64 x86_64 GNU/Linux

root@localhost:/# cat /sys/module/apparmor/parameters/enabled
Y

root@localhost:/# apparmor_status
apparmor module is loaded.
5 profiles are loaded.
5 profiles are in enforce mode.
   /usr/sbin/chronyd
   kata-default
   lsb_release
   nvidia_modprobe
   nvidia_modprobe//kmod
0 profiles are in complain mode.
4 processes have profiles defined.
4 processes are in enforce mode.
   /usr/sbin/chronyd (121)
   /usr/sbin/chronyd (122)
   /usr/bin/dash (128) kata-default
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.
root@localhost:/#

2. Launch Kata containers with AppArmor (custom profile)

When you want to apply a custom AppArmor profile rather than the kata-default, you can use the guest_apparmor_profile in the runtime configuration.
For this experiment, we can try to apply the nvidia_modprobe as a custom profile, which is provided
the apparmor package and stored under /etc/apparmor.d in the guest rootfs by default.

Note:

  • The nvidia_modprobe is not a suitable profile for container processes.
    Hence, this experiment is for verifying the guest_apparmor_profile works properly.
  • If you want to apply your custom profile, you need to put the profile under /etc/apparmor.d in the
    guest rootfs in advance.

Specify nvida_modprobe to the guest_apparmor_profile.

$ sudo sed -i -e 's/^# *\(guest_apparmor_profile\).*=.*$/\1="nvidia_modprobe"/g' /etc/kata-containers/configuration.toml

Or use the pod-level annotation in your sandbox.json.

"annotations": {
    "io.katacontainers.config.runtime.guest_apparmor_profile": "nvidia_modprobe"
}

Launch a container using crictl

$ sudo crictl run -r kata container.json sandbox.json
d85f905ed2aebe5ea60dcdea65d06473b42f384ee8f018b20aa5f5d4d878be83

Attach the Kata VM

You can confirm that the nvidia_modprobe profile is loaded and applied to /pause and /usr/bin/dash processes
instead of the kata-default.

$ sudo kata-runtime exec 2aeca734667dc9b1bd24f544dbcf2701dfbdd8ae885cd44cf6ea16ce51a7f0ca

root@localhost:/# cat /sys/module/apparmor/parameters/enabled
Y

root@localhost:/# apparmor_status
apparmor module is loaded.
5 profiles are loaded.
5 profiles are in enforce mode.
   /usr/sbin/chronyd
   kata-default
   lsb_release
   nvidia_modprobe
   nvidia_modprobe//kmod
0 profiles are in complain mode.
4 processes have profiles defined.
4 processes are in enforce mode.
   /usr/sbin/chronyd (121)
   /usr/sbin/chronyd (122)
   /usr/bin/dash (128) nvidia_modprobe
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.

Firecracker

I couldn't launch Kata Containers with firecracker via crictl (CRI), so I used ctr instead.

Launch Kata containers with AppArmor (default profile kata-default)

Launch a container using ctr

$ sudo ctr run --snapshotter devmapper --runtime io.containerd.run.kata.v2 -t --rm docker.io/library/busybox:latest kata-test sh

Attach the Kata VM

You can confirm that the kata-default profile is loaded and applied to /bin/dash process.

$ sudo kata-runtime exec kata-test
root@localhost:/# apparmor_status
apparmor module is loaded.
5 profiles are loaded.
5 profiles are in enforce mode.
   /usr/sbin/chronyd
   kata-default
   lsb_release
   nvidia_modprobe
   nvidia_modprobe//kmod
0 profiles are in complain mode.
3 processes have profiles defined.
3 processes are in enforce mode.
   /usr/sbin/chronyd (129)
   /usr/sbin/chronyd (131)
   /bin/dash (139) kata-default
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.

@ManaSugi
Copy link
Member Author

ManaSugi commented Aug 8, 2023

/cc @fidencio @amshinde @liubin

@ManaSugi ManaSugi added no-backport-needed area/agent Impacts the agent in the virtual machine area/runtime Issues that impact the runtime (including shimv2) area/osbuilder Issues related to the osbuilder script kernel Related to the guest kernel labels Aug 8, 2023
@ManaSugi
Copy link
Member Author

ManaSugi commented Aug 9, 2023

/test

@ManaSugi ManaSugi force-pushed the support-apparmor branch 3 times, most recently from 520ed53 to a1593d4 Compare August 13, 2023 14:24
@katacontainersbot katacontainersbot added size/tiny Smallest and simplest task and removed size/large Task of significant size labels Aug 13, 2023
@ManaSugi
Copy link
Member Author

/test

@katacontainersbot katacontainersbot added size/large Task of significant size and removed size/tiny Smallest and simplest task labels Aug 14, 2023
@ManaSugi
Copy link
Member Author

/test

@ManaSugi
Copy link
Member Author

/test-power

@ManaSugi
Copy link
Member Author

/test-dragonball

@ManaSugi
Copy link
Member Author

/test-power

@katacontainersbot katacontainersbot added size/tiny Smallest and simplest task and removed size/large Task of significant size labels Aug 14, 2023
@ManaSugi
Copy link
Member Author

/test

ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 20, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 20, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 21, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 22, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 22, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
ManaSugi added a commit to ManaSugi/tests that referenced this pull request Aug 22, 2023
Add a test case which check whether AppArmor inside the guest
works properly using containerd.

The test creates a container configured to apply the `kata-default`
profile, then it checks the container process is running with the
profile enforced.

Fixes: kata-containers#5748
Depends-on: github.com/kata-containers/kata-containers#7587

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
Pass AppArmor profile for containers to the agent if
`disable_guest_apparmor` is set to `false` in the runtime configuration
or `io.katacontainers.config.hypervisor.disable_guest_apparmor` is set
to `false` in the K8s yaml file.
The 'kata-default' based on the containerd's default profile is applied
to the container process inside the guest by default.
Users can also set a custom AppArmor profile to the container process using
`guest_apparmor_profile` in the runtime configuration or
`io.katacontainers.config.runtime.guest_apparmor_profile` in the K8s yaml file.
This will be an alternative configuration of Kubernetes' annotation,
`container.apparmor.security.beta.kubernetes.io` for AppArmor because
users cannot apply AppArmor profiles to the container via Kubernetes'
configuration. To apply the profile to the container, the guest rootfs
must be Ubuntu or Debian that is created with `APPARMOR`=yes.

Fixes: kata-containers#7586

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
Pass AppArmor profile for containers to the agent if
`disable_guest_apparmor` is set to `false` in the runtime configuration
or `io.katacontainers.config.hypervisor.disable_guest_apparmor` is set
to `false` in the K8s yaml file.
The 'kata-default' based on the containerd's default profile is applied
to the container process inside the guest by default.
Users can also set a custom AppArmor profile to the container process using
`guest_apparmor_profile` in the runtime configuration or
`io.katacontainers.config.runtime.guest_apparmor_profile` in the K8s yaml
This will be an alternative configuration of Kubernetes' annotation,
`container.apparmor.security.beta.kubernetes.io` for AppArmor because
users cannot apply AppArmor profiles to the container via Kubernetes'
configuration. To apply the profile to the container, the guest rootfs
must be Ubuntu or Debian that is created with `APPARMOR`=yes.

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
Add the description about how to enable AppArmor for containers
running inside the guest.

Fixes: kata-containers#7586

Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com>
@ManaSugi
Copy link
Member Author

/test

@ManaSugi
Copy link
Member Author

/test-dragonball

1 similar comment
@ManaSugi
Copy link
Member Author

/test-dragonball

@ManaSugi
Copy link
Member Author

Updates:
The previous implementation had applied AppArmor profiles to the pause container, but this isn’t right behavior. So if the ContainerType is PodSandbox, applying the profile is skipped.

Besides, the test kata-containers/tests#5749 passed. This PR is ready for the merge.

@ManaSugi
Copy link
Member Author

@fidencio @amshinde @bergwolf @jiangliu
I'd appreciate if you could review this when you have time.

After this is merged into the mainline, I'll prepare for adding the disable_guest_apparmor option to the remote hypervisor like confidential-containers/cloud-api-adaptor#439.

@fidencio
Copy link
Member

I'd appreciate if you could review this when you have time.

I'm adding thsi to my list, but it won't happen during this week, sorry.
Next week I will get to this one.

Copy link

This PR has been opened without with no activity for 180 days. Comment on the issue otherwise it will be closed in 7 days

@github-actions github-actions bot added the stale Issue or PR was not updated in a timely fashion label Feb 28, 2024
@ManaSugi ManaSugi removed the stale Issue or PR was not updated in a timely fashion label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/agent Impacts the agent in the virtual machine area/osbuilder Issues related to the osbuilder script area/runtime Issues that impact the runtime (including shimv2) kernel Related to the guest kernel size/huge Largest and most complex task (probably needs breaking into small pieces)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

runtime,agent: Add AppArmor support on the guest side
5 participants