-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples/bpf: add userspace example for prohibiting sockets
Add examples preventing a process in a cgroup from opening a socket based family, protocol and type. Signed-off-by: David Ahern <dsa@cumulusnetworks.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
- Loading branch information
David Ahern
authored and
David S. Miller
committed
Dec 2, 2016
1 parent
4f2e7ae
commit 554ae6e
Showing
4 changed files
with
195 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <uapi/linux/bpf.h> | ||
#include <linux/socket.h> | ||
#include <linux/net.h> | ||
#include <uapi/linux/in.h> | ||
#include <uapi/linux/in6.h> | ||
#include "bpf_helpers.h" | ||
|
||
SEC("cgroup/sock1") | ||
int bpf_prog1(struct bpf_sock *sk) | ||
{ | ||
char fmt[] = "socket: family %d type %d protocol %d\n"; | ||
|
||
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol); | ||
|
||
/* block PF_INET6, SOCK_RAW, IPPROTO_ICMPV6 sockets | ||
* ie., make ping6 fail | ||
*/ | ||
if (sk->family == PF_INET6 && | ||
sk->type == SOCK_RAW && | ||
sk->protocol == IPPROTO_ICMPV6) | ||
return 0; | ||
|
||
return 1; | ||
} | ||
|
||
SEC("cgroup/sock2") | ||
int bpf_prog2(struct bpf_sock *sk) | ||
{ | ||
char fmt[] = "socket: family %d type %d protocol %d\n"; | ||
|
||
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol); | ||
|
||
/* block PF_INET, SOCK_RAW, IPPROTO_ICMP sockets | ||
* ie., make ping fail | ||
*/ | ||
if (sk->family == PF_INET && | ||
sk->type == SOCK_RAW && | ||
sk->protocol == IPPROTO_ICMP) | ||
return 0; | ||
|
||
return 1; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* 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); | ||
if (ret < 0) { | ||
printf("Failed to attach prog to cgroup: '%s'\n", | ||
strerror(errno)); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/bin/bash | ||
|
||
function config_device { | ||
ip netns add at_ns0 | ||
ip link add veth0 type veth peer name veth0b | ||
ip link set veth0b up | ||
ip link set veth0 netns at_ns0 | ||
ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0 | ||
ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad | ||
ip netns exec at_ns0 ip link set dev veth0 up | ||
ip addr add 172.16.1.101/24 dev veth0b | ||
ip addr add 2401:db00::2/64 dev veth0b nodad | ||
} | ||
|
||
function config_cgroup { | ||
rm -rf /tmp/cgroupv2 | ||
mkdir -p /tmp/cgroupv2 | ||
mount -t cgroup2 none /tmp/cgroupv2 | ||
mkdir -p /tmp/cgroupv2/foo | ||
echo $$ >> /tmp/cgroupv2/foo/cgroup.procs | ||
} | ||
|
||
|
||
function attach_bpf { | ||
test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1 | ||
[ $? -ne 0 ] && exit 1 | ||
} | ||
|
||
function cleanup { | ||
ip link del veth0b | ||
ip netns delete at_ns0 | ||
umount /tmp/cgroupv2 | ||
rm -rf /tmp/cgroupv2 | ||
} | ||
|
||
cleanup 2>/dev/null | ||
|
||
set -e | ||
config_device | ||
config_cgroup | ||
set +e | ||
|
||
# | ||
# Test 1 - fail ping6 | ||
# | ||
attach_bpf 0 | ||
ping -c1 -w1 172.16.1.100 | ||
if [ $? -ne 0 ]; then | ||
echo "ping failed when it should succeed" | ||
cleanup | ||
exit 1 | ||
fi | ||
|
||
ping6 -c1 -w1 2401:db00::1 | ||
if [ $? -eq 0 ]; then | ||
echo "ping6 succeeded when it should not" | ||
cleanup | ||
exit 1 | ||
fi | ||
|
||
# | ||
# Test 2 - fail ping | ||
# | ||
attach_bpf 1 | ||
ping6 -c1 -w1 2401:db00::1 | ||
if [ $? -ne 0 ]; then | ||
echo "ping6 failed when it should succeed" | ||
cleanup | ||
exit 1 | ||
fi | ||
|
||
ping -c1 -w1 172.16.1.100 | ||
if [ $? -eq 0 ]; then | ||
echo "ping succeeded when it should not" | ||
cleanup | ||
exit 1 | ||
fi | ||
|
||
cleanup | ||
echo | ||
echo "*** PASS ***" |