tools/testing/selftests/bpf/progs/lsm_cgroup.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/lsm_cgroup.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/lsm_cgroup.c- Extension
.c- Size
- 4929 bytes
- Lines
- 223
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.hbpf_tracing_net.hbpf/bpf_helpers.hbpf/bpf_tracing.h
Detected Declarations
function test_local_storagefunction real_createfunction BPF_PROGfunction BPF_PROGfunction real_bindfunction BPF_PROGfunction BPF_PROGfunction BPF_PROGfunction BPF_PROGfunction BPF_PROGfunction BPF_PROGfunction BPF_PROGfunction BPF_PROG
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#include "vmlinux.h"
#include "bpf_tracing_net.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
char _license[] SEC("license") = "GPL";
extern bool CONFIG_SECURITY_SELINUX __kconfig __weak;
extern bool CONFIG_SECURITY_SMACK __kconfig __weak;
extern bool CONFIG_SECURITY_APPARMOR __kconfig __weak;
#ifndef AF_PACKET
#define AF_PACKET 17
#endif
#ifndef AF_UNIX
#define AF_UNIX 1
#endif
#ifndef EPERM
#define EPERM 1
#endif
struct {
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
__type(key, __u64);
__type(value, __u64);
} cgroup_storage SEC(".maps");
int called_socket_post_create;
int called_socket_post_create2;
int called_socket_bind;
int called_socket_bind2;
int called_socket_alloc;
int called_socket_clone;
int skipcap_retval = -4095;
int socket_retval = -4095;
static __always_inline int test_local_storage(void)
{
__u64 *val;
val = bpf_get_local_storage(&cgroup_storage, 0);
if (!val)
return 0;
*val += 1;
return 1;
}
static __always_inline int real_create(struct socket *sock, int family,
int protocol)
{
struct sock *sk;
int prio = 123;
/* Reject non-tx-only AF_PACKET. */
if (family == AF_PACKET && protocol != 0)
return 0; /* EPERM */
sk = sock->sk;
if (!sk)
return 1;
/* The rest of the sockets get default policy. */
if (bpf_setsockopt(sk, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
return 0; /* EPERM */
/* Make sure bpf_getsockopt is allowed and works. */
prio = 0;
if (bpf_getsockopt(sk, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
return 0; /* EPERM */
if (prio != 123)
return 0; /* EPERM */
/* Can access cgroup local storage. */
if (!test_local_storage())
return 0; /* EPERM */
return 1;
}
/* __cgroup_bpf_run_lsm_socket */
SEC("lsm_cgroup/socket_post_create")
int BPF_PROG(socket_post_create, struct socket *sock, int family,
int type, int protocol, int kern)
{
called_socket_post_create++;
Annotation
- Immediate include surface: `vmlinux.h`, `bpf_tracing_net.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`.
- Detected declarations: `function test_local_storage`, `function real_create`, `function BPF_PROG`, `function BPF_PROG`, `function real_bind`, `function BPF_PROG`, `function BPF_PROG`, `function BPF_PROG`, `function BPF_PROG`, `function BPF_PROG`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.