forked from Minki/linux
7f67763337
If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
to the given cgroup the descendent cgroup will be able to override
effective bpf program that was inherited from this cgroup.
By default it's not passed, therefore override is disallowed.
Examples:
1.
prog X attached to /A with default
prog Y fails to attach to /A/B and /A/B/C
Everything under /A runs prog X
2.
prog X attached to /A with allow_override.
prog Y fails to attach to /A/B with default (non-override)
prog M attached to /A/B with allow_override.
Everything under /A/B runs prog M only.
3.
prog X attached to /A with allow_override.
prog Y fails to attach to /A with default.
The user has to detach first to switch the mode.
In the future this behavior may be extended with a chain of
non-overridable programs.
Also fix the bug where detach from cgroup where nothing is attached
was not throwing error. Return ENOENT in such case.
Add several testcases and adjust libbpf.
Fixes: 3007098494
("cgroup: add support for eBPF programs")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Daniel Mack <daniel@zonque.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
67 lines
1.3 KiB
C
67 lines
1.3 KiB
C
/* eBPF example program:
|
|
*
|
|
* - Loads eBPF program
|
|
*
|
|
* The eBPF program loads a filter from file and attaches the
|
|
* program to a cgroup using BPF_PROG_ATTACH
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <net/if.h>
|
|
#include <linux/bpf.h>
|
|
|
|
#include "libbpf.h"
|
|
#include "bpf_load.h"
|
|
|
|
static int usage(const char *argv0)
|
|
{
|
|
printf("Usage: %s cg-path filter-path [filter-id]\n", argv0);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int cg_fd, ret, filter_id = 0;
|
|
|
|
if (argc < 3)
|
|
return usage(argv[0]);
|
|
|
|
cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY);
|
|
if (cg_fd < 0) {
|
|
printf("Failed to open cgroup path: '%s'\n", strerror(errno));
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
if (load_bpf_file(argv[2]))
|
|
return EXIT_FAILURE;
|
|
|
|
printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf);
|
|
|
|
if (argc > 3)
|
|
filter_id = atoi(argv[3]);
|
|
|
|
if (filter_id > prog_cnt) {
|
|
printf("Invalid program id; program not found in file\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
ret = bpf_prog_attach(prog_fd[filter_id], cg_fd,
|
|
BPF_CGROUP_INET_SOCK_CREATE, 0);
|
|
if (ret < 0) {
|
|
printf("Failed to attach prog to cgroup: '%s'\n",
|
|
strerror(errno));
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|