Linux Audio

Check our new training course

Loading...
v6.2
 1// SPDX-License-Identifier: GPL-2.0
 2#include <linux/bpf.h>
 3#include <bpf/bpf_helpers.h>
 4#include <bpf/bpf_tracing.h>
 5#include <errno.h>
 6#include <linux/capability.h>
 7
 8struct kernel_cap_struct {
 9	__u32 cap[_LINUX_CAPABILITY_U32S_3];
10} __attribute__((preserve_access_index));
11
12struct cred {
13	struct kernel_cap_struct cap_effective;
14} __attribute__((preserve_access_index));
15
16char _license[] SEC("license") = "GPL";
17
18SEC("lsm.s/userns_create")
19int BPF_PROG(test_userns_create, const struct cred *cred, int ret)
20{
21	struct kernel_cap_struct caps = cred->cap_effective;
22	int cap_index = CAP_TO_INDEX(CAP_SYS_ADMIN);
23	__u32 cap_mask = CAP_TO_MASK(CAP_SYS_ADMIN);
24
25	if (ret)
26		return 0;
27
28	ret = -EPERM;
29	if (caps.cap[cap_index] & cap_mask)
30		return 0;
31
32	return -EPERM;
33}
v6.9.4
 1// SPDX-License-Identifier: GPL-2.0
 2#include <linux/bpf.h>
 3#include <bpf/bpf_helpers.h>
 4#include <bpf/bpf_tracing.h>
 5#include <errno.h>
 6#include <linux/capability.h>
 7
 8typedef struct { unsigned long long val; } kernel_cap_t;
 
 
 9
10struct cred {
11	kernel_cap_t cap_effective;
12} __attribute__((preserve_access_index));
13
14char _license[] SEC("license") = "GPL";
15
16SEC("lsm.s/userns_create")
17int BPF_PROG(test_userns_create, const struct cred *cred, int ret)
18{
19	kernel_cap_t caps = cred->cap_effective;
20	__u64 cap_mask = 1ULL << CAP_SYS_ADMIN;
 
21
22	if (ret)
23		return 0;
24
25	ret = -EPERM;
26	if (caps.val & cap_mask)
27		return 0;
28
29	return -EPERM;
30}