-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
epbf: Propagate PID namespace to kernel space (#3008)
* epbf: Propagate PID namespace to kernel space If grafana-agent is running on another namespace than the host PID namespace, then the PID that are selected by Grafana Agent are not matching up with the ones collected. This will respect the PID namespace the Grafana Agent is running under. Fixes #3002 #1994 * Fix child pid namespaces Replace use bpf_get_ns_current_pid_tgid, as it only matches in the exact same namespace and not child namespaces. This iterates up to 4 namespace levels up to find a match. * Only compare inode * Remove files added in error * Read nr using BPF_CORE_READ macro * Correctly use group_leader
- Loading branch information
1 parent
6a47d01
commit d58b751
Showing
14 changed files
with
96 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,39 @@ | ||
|
||
#if !defined(PYROSCOPE_PID) | ||
#define PYROSCOPE_PID | ||
|
||
// this should not be used in production, and always be disabled | ||
// but is useful for running in a privileged context outside host pid namespace, for example wsl2 | ||
//#define PYROSCOPE_PID_NAMESPACED | ||
|
||
#if defined(PYROSCOPE_PID_NAMESPACED) | ||
|
||
#include "bpf_core_read.h" | ||
// https://github.com/grafana/beyla/blob/6366275ce2d2c9bdefd47975b389fbcf39cbbea8/bpf/pid.h#L13 | ||
// Good resource on this: https://mozillazg.com/2022/05/ebpf-libbpfgo-get-process-info-en.html | ||
// Using bpf_get_ns_current_pid_tgid is too restrictive for us | ||
//static __always_inline void ns_pid_ppid(struct task_struct *task, u32 *pid , int *ppid, u32 *pid_ns_id) { | ||
static __always_inline void current_pid(u32 *pid) { | ||
struct task_struct *task = (struct task_struct *)bpf_get_current_task(); | ||
if (task == 0) { | ||
return; | ||
} | ||
struct upid upid; | ||
|
||
unsigned int level = BPF_CORE_READ(task, nsproxy, pid_ns_for_children, level); | ||
struct pid *ns_pid = (struct pid *)BPF_CORE_READ(task, group_leader, thread_pid); | ||
bpf_probe_read_kernel(&upid, sizeof(upid), &ns_pid->numbers[level]); | ||
|
||
*pid = (u32)upid.nr; | ||
// unsigned int p_level = BPF_CORE_READ(task, real_parent, nsproxy, pid_ns_for_children, level); | ||
// | ||
// struct pid *ns_ppid = (struct pid *)BPF_CORE_READ(task, real_parent, group_leader, thread_pid); | ||
// bpf_probe_read_kernel(&upid, sizeof(upid), &ns_ppid->numbers[p_level]); | ||
// *ppid = upid.nr; | ||
// | ||
// struct ns_common ns = BPF_CORE_READ(task, nsproxy, pid_ns_for_children, ns); | ||
// *pid_ns_id = ns.inum; | ||
} | ||
|
||
#else // PYROSCOPE_PID_NAMESPACED | ||
|
||
static __always_inline void current_pid(u32 *pid) { | ||
u64 pid_tgid = bpf_get_current_pid_tgid(); | ||
*pid = (u32)(pid_tgid >> 32); | ||
} | ||
#endif // PYROSCOPE_PID_NAMESPACED | ||
|
||
|
||
#endif // PYROSCOPE_PID | ||
#if !defined(PYROSCOPE_PID) | ||
#define PYROSCOPE_PID | ||
|
||
#include "bpf_core_read.h" | ||
#include "bpf_helpers.h" | ||
#include "vmlinux.h" | ||
|
||
#define PID_NESTED_NAMESPACES_MAX 4 | ||
|
||
static __always_inline void current_pid(uint64_t ns_pid_ino, uint32_t *pid) { | ||
unsigned int inum; | ||
|
||
// fallback to host pid, if no inode provided | ||
if (ns_pid_ino == 0) { | ||
uint64_t pid_tgid = bpf_get_current_pid_tgid(); | ||
*pid = (u32)(pid_tgid >> 32); | ||
return; | ||
} | ||
|
||
struct task_struct *task = (struct task_struct *)bpf_get_current_task(); | ||
|
||
// retrieve level nested namespaces | ||
unsigned int level = BPF_CORE_READ(task, group_leader, nsproxy, pid_ns_for_children, level); | ||
|
||
// match the level with pid ns inode | ||
#pragma unroll | ||
for (int i = 0; i < PID_NESTED_NAMESPACES_MAX; i++) { | ||
if ((level - i) < 0) { | ||
break; | ||
} | ||
inum = BPF_CORE_READ(task, group_leader, thread_pid, numbers[level - i].ns, ns.inum); | ||
if (inum == ns_pid_ino) { | ||
*pid = BPF_CORE_READ(task, group_leader, thread_pid, numbers[level - i].nr); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
#endif // PYROSCOPE_PID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pyrobpf | ||
|
||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type pid_event -target amd64 -cc clang -cflags "-O2 -Wall -Werror -fpie -Wno-unused-variable -Wno-unused-function" Profile ../bpf/profile.bpf.c -- -I../bpf/libbpf -I../bpf/vmlinux/ | ||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type pid_event -target arm64 -cc clang -cflags "-O2 -Wall -Werror -fpie -Wno-unused-variable -Wno-unused-function" Profile ../bpf/profile.bpf.c -- -I../bpf/libbpf -I../bpf/vmlinux/ | ||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type global_config_t -type pid_event -target amd64 -cc clang -cflags "-O2 -Wall -Werror -fpie -Wno-unused-variable -Wno-unused-function" Profile ../bpf/profile.bpf.c -- -I../bpf/libbpf -I../bpf/vmlinux/ | ||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type global_config_t -type pid_event -target arm64 -cc clang -cflags "-O2 -Wall -Werror -fpie -Wno-unused-variable -Wno-unused-function" Profile ../bpf/profile.bpf.c -- -I../bpf/libbpf -I../bpf/vmlinux/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters