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

Introduce documentation about required capabilities #1587

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions docs/sources/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ While most eBPF programs require elevated privileges, Beyla allow you to specify

Some Beyla functionality requires further permissions, for example using the network observability probes with Linux Traffic Control requires `CAP_NET_ADMIN`, but it's a feature you have to optionally enable.

`CAP_SYS_ADMIN` is only required to enable context propagation in Go across multiple nodes, however if this permission
isn't granted Beyla gracefully degrades its functionality to support only partial traces.

Distributed tracing is only supported for Go services, while other programming language support remains on the road-map. Distributed tracing for other programming languages works on the same node, but not across different nodes.
For a comprehensive list of capabilities required by Beyla, refer to [Security, permissions and capabilities]({{< relref "./security" >}}).

## Get started

Expand Down
172 changes: 172 additions & 0 deletions docs/sources/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
---
title: Security, permissions and capabilities
menuTitle: Security, permissions and capabilities
description: Privileges and capabilities required by Beyla
weight: 22
keywords:
- Beyla
- eBPF
- security
- capabilities
aliases:
- /docs/grafana-cloud/monitor-applications/beyla/security/
---

# Security, permissions and capabilities
rafaelroquetto marked this conversation as resolved.
Show resolved Hide resolved

Beyla needs access to various Linux interfaces to instrument applications, such as reading from the `/proc` filesystem, loading _eBPF_ programs, and managing network interface filters. Many of these operations require elevated permissions due to their nature. The simplest solution is to run Beyla as `root`, which gives it all the permissions it needs. However, this might not work well in more complex setups where full `root` access isn’t ideal. To address this, Beyla is designed to use only the specific Linux kernel capabilities needed for its current configuration.
grafsean marked this conversation as resolved.
Show resolved Hide resolved

## Linux kernel capabilities

Linux kernel capabilities are a fine-grained system for controlling access to privileged operations on the system. They allow you to grant specific permissions to processes without giving them full root (superuser) access, which helps improve security by adhering to the principle of least privilege. Instead of relying on the traditional all-or-nothing root model, capabilities split the powerful privileges typically associated with root into smaller, more manageable units.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add link some here to some official docs or so?


Each capability corresponds to a particular privileged operation in the kernel.

Capabilities are assigned to processes and executable files. By using tools like `setcap`, administrators can assign specific capabilities to a binary, enabling it to perform only the operations it needs without running as `root`. For example:

```bash
sudo setcap cap_net_admin,cap_net_raw+ep myprogram
```

This example grants the `CAP_NET_ADMIN` and `CAP_NET_RAW` capabilities to `myprogram`, allowing it to manage network settings without requiring full superuser privileges.

By choosing and assigning capabilities carefully, you can lower the risk of privilege escalation while still letting processes do what they need to.

## Beyla operation modes

grafsean marked this conversation as resolved.
Show resolved Hide resolved
Beyla can operate in two distinct modes: _application observability_ and _network observability_. These modes are not mutually exclusive and can be used together as needed. The former is typically enabled using the `BEYLA_OPEN_PORT` or `BEYLA_EXECUTABLE_NAME` options, while the latter is controlled via the `BEYLA_NETWORK_METRICS` option. For more details on enabling these modes, refer to the [configuration documentation](/docs/beyla/latest/configure/options/).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of namig BEYLA_OPEN_PORT ... can we please refer to the docs on the discovery section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea


What Beyla requires in terms of capabilities depends entirely on the modes and features you enable. Conversely, the capabilities you provide determine what Beyla is able to do.

When starting up, Beyla reads its configuration and checks for the required capabilities. If any are missing, Beyla displays a warning like the following:

```
time=2025-01-27T17:21:20.197-06:00 level=WARN msg="Required system capabilities not present, Beyla may malfunction" error="the following capabilities are required: CAP_DAC_READ_SEARCH, CAP_BPF, CAP_CHECKPOINT_RESTORE"
```

Beyla then attempts to continue running, but missing capabilities may lead to errors later on.

To prevent this, you can set `BEYLA_ENFORCE_SYS_CAPS=1`, which causes Beyla to fail immediately if the required capabilities are not available. In this case, it terminates right after printing the warning message above.

## List of capabilities required by Beyla

Below is a list of capabilities and their usage in the context of Beyla

| Capability | Usage in Beyla |
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| `CAP_BPF` | enables general BPF functionality and `BPF_PROG_TYPE_SOCK_FILTER` programs |
| `CAP_NET_RAW` | used to create `AF_PACKET` raw sockets |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is needed for the socket filter program. I think we need to be more descriptive here about how the capabilities relate to Beyla features, I don't think our users would know what AF_PACKET means in terms of Beyla. Same for NET_ADMIN, we need to say network observability and context propagation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I will reshuffle this.

| `CAP_NET_ADMIN` | required to load `BPF_PROG_TYPE_SCHED_CLS` (tc) programs |
rafaelroquetto marked this conversation as resolved.
Show resolved Hide resolved
| `CAP_PERFMON` | direct packet access and pointer arithmetic and loading `BPF_PROG_TYPE_KPROBE` programs |
| `CAP_DAC_READ_SEARCH` | access to `/proc/self/mem` to determine kernel version |
| `CAP_CHECKPOINT_RESTORE` | access to symlinks in the `/proc` filesystem |
| `CAP_SYS_PTRACE` | access to `/proc/pid/exe` and friends |
grafsean marked this conversation as resolved.
Show resolved Hide resolved
| `CAP_SYS_RESOURCE` | _(kernels **< 5.11** only)_ increase the amount of locked memory available. |
| `CAP_SYS_ADMIN` | Library-level Go trace-context propagation via `bpf_probe_write_user()` and access to BTF data by the BPF metrics exporter |

**Note** Access to `CAP_PERFMON` is subject to `perf_events` access controls governed by the `kernel.perf_event_paranoid` kernel setting, which can adjusted via `sysctl` or by modifying the file `/proc/sys/kernel/perf_event_paranoid`.
The default setting for `kernel.perf_event_paranoid` is typically `2`, which is documented under the `perf_event_paranoid` section in the [kernel documentation](https://www.kernel.org/doc/Documentation/sysctl/kernel.txt) and more comprehensively under [the perf-security documentation](https://www.kernel.org/doc/Documentation/admin-guide/perf-security.rst).
Some Linux distributions define higher levels for `kernel.perf_event_paranoid`, for example Debian based distributions [also use](https://lwn.net/Articles/696216/) `kernel.perf_event_paranoid=3`,
which disallows access to `perf_event_open()` without `CAP_SYS_ADMIN`. If you are running on a distribution with `kernel.perf_event_paranoid` setting higher than `2`,
you can either modify your configuration to lower it to `2` or use `CAP_SYS_ADMIN` instead of `CAP_PERFMON`.

## Examples
grafsean marked this conversation as resolved.
Show resolved Hide resolved

The final set of required capabilities depends on the actual Beyla configuration and the type of tracers being used, as described in the section __Beyla operation modes__ above. Here are a few examples of how to run Beyla as a non-root user.

### Network Metrics (using a socket filter)

#### Required capabilities:
* `CAP_BPF`
* `CAP_NET_RAW`

```
sudo setcap cap_bpf,cap_net_raw+ep ./bin/beyla
BEYLA_NETWORK_METRICS=1 BEYLA_NETWORK_PRINT_FLOWS=1 bin/beyla
```

### Network Metrics (using traffic control)

#### Required capabilities:
* `CAP_BPF`
* `CAP_NET_ADMIN`
* `CAP_PERFMON`

```
sudo setcap cap_bpf,cap_net_admin,cap_perfmon+ep ./bin/beyla
BEYLA_NETWORK_METRICS=1 BEYLA_NETWORK_PRINT_FLOWS=1 BEYLA_NETWORK_SOURCE=tc bin/beyla
```

### Application observability

#### Required capabilities:
* `CAP_BPF`
* `CAP_DAC_READ_SEARCH`
* `CAP_CHECKPOINT_RESTORE`
* `CAP_PERFMON`
* `CAP_NET_RAW`
* `CAP_SYS_PTRACE`

```
sudo setcap cap_bpf,cap_dac_read_search,cap_perfmon,cap_net_raw,cap_sys_ptrace+ep ./bin/beyla
BEYLA_OPEN_PORT=8080 BEYLA_TRACE_PRINTER=text bin/beyla
```

### Application observability with trace context propagation

#### Required capabilities:
* `CAP_BPF`
* `CAP_DAC_READ_SEARCH`
* `CAP_CHECKPOINT_RESTORE`
* `CAP_PERFMON`
* `CAP_NET_RAW`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think NET_ADMIN assumes NET_RAW, maybe we don't need that one but it's fine to keep it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked the kernel sources, they are orthogonal to each other.

* `CAP_SYS_PTRACE`
* `CAP_NET_ADMIN`

```
sudo setcap cap_bpf,cap_dac_read_search,cap_perfmon,cap_net_raw,cap_sys_ptrace,cap_net_admin+ep ./bin/beyla
BEYLA_BPF_ENABLE_CONTEXT_PROPAGATION=1 BEYLA_OPEN_PORT=8080 BEYLA_TRACE_PRINTER=text bin/beyla
```

## Internal eBPF tracer capability requirement reference

Below is a list of internal eBPF tracers used by Beyla and their required capabilities
### Socket flow fetcher
- `CAP_BPF` -> for `BPF_PROG_TYPE_SOCK_FILTER`
- `CAP_NET_RAW` -> for creating `AF_PACKET` socket

### Flow fetcher (tc)
grafsean marked this conversation as resolved.
Show resolved Hide resolved
- `CAP_BPF`
- `CAP_NET_ADMIN` -> for `PROG_TYPE_SCHED_CLS`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to say what PROG_TYPE_SCHE... does, rather than saying it's for that.

- `CAP_PERFMON` -> direct access to `struct __sk_buff::data` and pointer arithmetic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again we need to say what this means for our end users.


### Watcher
- `CAP_BPF`
- `CAP_CHECKPOINT_RESTORE`
- `CAP_DAC_READ_SEARCH` -> access to `/proc/self/mem` to determine kernel version
- `CAP_PERFMON` -> for `BPF_PROG_TYPE_KPROBE`

### Generic tracer
grafsean marked this conversation as resolved.
Show resolved Hide resolved
- `CAP_BPF`
- `CAP_DAC_READ_SEARCH`
- `CAP_CHECKPOINT_RESTORE`
- `CAP_PERFMON`
- `CAP_NET_RAW` -> for creating `AF_PACKET` socket used by `beyla_socket__http_filter`
- `CAP_SYS_PTRACE` -> access to `/proc/pid/exe` and other nodes in `/proc`

### TC tracers
grafsean marked this conversation as resolved.
Show resolved Hide resolved
grafsean marked this conversation as resolved.
Show resolved Hide resolved
* `CAP_BPF`
* `CAP_DAC_READ_SEARCH`
* `CAP_PERFMON`
* `CAP_NET_ADMIN` -> for `BPF_PROG_TYPE_SCHED_CLS`, `BPF_PROG_TYPE_SOCK_OPS` and `BPF_PROG_TYPE_SK_MSG`

### GO tracer
grafsean marked this conversation as resolved.
Show resolved Hide resolved
- `CAP_BPF`
- `CAP_DAC_READ_SEARCH`
- `CAP_CHECKPOINT_RESTORE`
- `CAP_PERFMON`
- `CAP_NET_RAW` -> for creating `AF_PACKET` socket used by `beyla_socket__http_filter`
- `CAP_SYS_PTRACE` -> access to `/proc/pid/exe` and other nodes in `/proc`
- `CAP_SYS_ADMIN` -> for probe based (`bpf_probe_write_user()`) library level context propagation


19 changes: 3 additions & 16 deletions docs/sources/setup/kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,24 +251,11 @@ In all of the examples so far, `privileged:true` or the `SYS_ADMIN` Linux capabi

The following guide is based on tests performed mainly by running `containerd` with `GKE`, `kubeadm`, `k3s`, `microk8s` and `kind`.

To run Beyla unprivileged, you need to run a `privileged` init container which performs setup tasks which require elevated privileges. Then you need to replace the `privileged:true` setting with a set of Linux [capabilities](https://www.man7.org/linux/man-pages/man7/capabilities.7.html).
To run Beyla unprivileged, you need to run a `privileged` init container which performs setup tasks which require elevated privileges. Then you need to replace the `privileged:true` setting with a set of Linux [capabilities](https://www.man7.org/linux/man-pages/man7/capabilities.7.html). A comprehensive list of capabilities required by Beyla can be found in [Security, permissions and capabilities]({{< relref "../security" >}}).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to run a privileged init container anymore, it should be removed as well.


- `CAP_BPF` is required to install most of the eBPF probes, because Beyla tracks system calls.
- `CAP_SYS_PTRACE` is required so that Beyla is able to look into the processes namespaces and inspect the executables. Beyla doesn't use `ptrace`, but for some of the operations it does require this capability.
- `CAP_NET_RAW` is required for using installing socket filters, which are used as a fallback for `kretprobes` for HTTP requests.
- `CAP_CHECKPOINT_RESTORE` is required to open ELF files.
- `CAP_DAC_READ_SEARCH` is required to open ELF files.
- `CAP_PERFMON` is required to load BPF programs, i.e. be able to perform `perf_event_open()`.
- `CAP_SYS_RESOURCE` is required only on kernels **< 5.11** so that Beyla can increase the amount of locked memory available.
In addition to these Linux capabilities, many Kubernetes versions include [AppArmour](https://kubernetes.io/docs/tutorials/security/apparmor/), which tough policies adds additional restrictions to unprivileged containers. By [default](https://github.com/moby/moby/blob/master/profiles/apparmor/template.go), the AppArmour policy restricts the use of `mount` and the access to `/sys/fs/` directories. Beyla requires access to `/sys/fs/cgroup/`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on what we saw, I think /cgroup is not restricted by the app armour default config. We need to confirm and remove this section too.


In addition to these Linux capabilities, many Kubernetes versions include [AppArmour](https://kubernetes.io/docs/tutorials/security/apparmor/), which tough policies adds additional restrictions to unprivileged containers. By [default](https://github.com/moby/moby/blob/master/profiles/apparmor/template.go), the AppArmour policy restricts the use of `mount` and the access to `/sys/fs/` directories. Beyla uses the BPF Linux file system to store pinned BPF maps, for communication among the different BPF programs. For this reason, Beyla either needs to `mount` a BPF file system, or write to `/sys/fs/bpf`, which are both restricted.

Because of the AppArmour restriction, to run Beyla as unprivileged container, you need to either:

- Set `container.apparmor.security.beta.kubernetes.io/beyla: "unconfined"` in your Kubernetes deployment files.
- Set a modified AppArmour policy which allows Beyla to perform `mount`.

**Note** Since the `beyla` container does not have the privileges required to mount or un-mount the BPF filesystem, this sample leaves the BPF filesystem mounted on the host, even after the sample is deleted. This samples uses a unique path for each namespace to ensure re-use the same mount if Beyla is re-deployed, but to avoid collisions if multiple instances of Beyla is run in different namespaces.
Because of the AppArmour restriction, to run Beyla as unprivileged container, you may need to set `container.apparmor.security.beta.kubernetes.io/beyla: "unconfined"` in your Kubernetes deployment files in case you run into issues.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [vale] reported by reviewdog 🐶
[Grafana.Kubernetes] Use 'Kubernetes Deployment' instead of 'Kubernetes deployment'.

For more information, refer to https://grafana.com/docs/writers-toolkit/write/style-guide/capitalization-punctuation/#kubernetes-objects.

If the rule is incorrect or needs improving, report an issue.

If you have reason to diverge from the style guidance, to skip a rule, refer to Skip rules.


**Note** Loading BPF programs requires that Beyla is able to read the Linux performance events, or at least be able to execute the Linux Kernel API `perf_event_open()`.

Expand Down
Loading