Files
linux/tools/testing/selftests/bpf/progs/test_task_under_cgroup.c
Blaise Boscaccy 082f1db02c security: Propagate caller information in bpf hooks
Certain bpf syscall subcommands are available for usage from both
userspace and the kernel. LSM modules or eBPF gatekeeper programs may
need to take a different course of action depending on whether or not
a BPF syscall originated from the kernel or userspace.

Additionally, some of the bpf_attr struct fields contain pointers to
arbitrary memory. Currently the functionality to determine whether or
not a pointer refers to kernel memory or userspace memory is exposed
to the bpf verifier, but that information is missing from various LSM
hooks.

Here we augment the LSM hooks to provide this data, by simply passing
a boolean flag indicating whether or not the call originated in the
kernel, in any hook that contains a bpf_attr struct that corresponds
to a subcommand that may be called from the kernel.

Signed-off-by: Blaise Boscaccy <bboscaccy@linux.microsoft.com>
Acked-by: Song Liu <song@kernel.org>
Acked-by: Paul Moore <paul@paul-moore.com>
Link: https://lore.kernel.org/r/20250310221737.821889-2-bboscaccy@linux.microsoft.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2025-03-15 11:48:58 -07:00

78 lines
1.6 KiB
C

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Bytedance */
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
long bpf_task_under_cgroup(struct task_struct *task, struct cgroup *ancestor) __ksym;
void bpf_cgroup_release(struct cgroup *p) __ksym;
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
void bpf_task_release(struct task_struct *p) __ksym;
const volatile int local_pid;
const volatile __u64 cgid;
int remote_pid;
SEC("tp_btf/task_newtask")
int BPF_PROG(tp_btf_run, struct task_struct *task, u64 clone_flags)
{
struct cgroup *cgrp = NULL;
struct task_struct *acquired;
if (local_pid != (bpf_get_current_pid_tgid() >> 32))
return 0;
acquired = bpf_task_acquire(task);
if (!acquired)
return 0;
if (local_pid == acquired->tgid)
goto out;
cgrp = bpf_cgroup_from_id(cgid);
if (!cgrp)
goto out;
if (bpf_task_under_cgroup(acquired, cgrp))
remote_pid = acquired->tgid;
out:
if (cgrp)
bpf_cgroup_release(cgrp);
bpf_task_release(acquired);
return 0;
}
SEC("lsm.s/bpf")
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
{
struct cgroup *cgrp = NULL;
struct task_struct *task;
int ret = 0;
task = bpf_get_current_task_btf();
if (local_pid != task->pid)
return 0;
if (cmd != BPF_LINK_CREATE)
return 0;
/* 1 is the root cgroup */
cgrp = bpf_cgroup_from_id(1);
if (!cgrp)
goto out;
if (!bpf_task_under_cgroup(task, cgrp))
ret = -1;
bpf_cgroup_release(cgrp);
out:
return ret;
}
char _license[] SEC("license") = "GPL";