Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
c9bf507
Documentation
LICENSES
arch
block
certs
crypto
drivers
firmware
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
accounting
arch
bpf
build
cgroup
firewire
gpio
hv
iio
include
kvm
laptop
leds
lib
memory-model
nfsd
objtool
pci
pcmcia
perf
power
scripts
spi
testing
fault-injection
ktest
nvdimm
radix-tree
scatterlist
selftests
android
bpf
gnu
include
.gitignore
Makefile
bpf_endian.h
bpf_flow.c
bpf_helpers.h
bpf_rand.h
bpf_rlimit.h
bpf_util.h
cgroup_helpers.c
cgroup_helpers.h
config
connect4_prog.c
connect6_prog.c
dev_cgroup.c
flow_dissector_load.c
get_cgroup_id_kern.c
get_cgroup_id_user.c
sample_map_ret0.c
sample_ret0.c
sendmsg4_prog.c
sendmsg6_prog.c
socket_cookie_prog.c
sockmap_parse_prog.c
sockmap_tcp_msg_prog.c
sockmap_verdict_prog.c
tcp_client.py
tcp_server.py
test_adjust_tail.c
test_align.c
test_btf.c
test_btf_haskv.c
test_btf_nokv.c
test_cgroup_storage.c
test_dev_cgroup.c
test_flow_dissector.c
test_flow_dissector.sh
test_get_stack_rawtp.c
test_iptunnel_common.h
test_kmod.sh
test_l4lb.c
test_l4lb_noinline.c
test_libbpf.sh
test_libbpf_open.c
test_lirc_mode2.sh
test_lirc_mode2_kern.c
test_lirc_mode2_user.c
test_lpm_map.c
test_lru_map.c
test_lwt_seg6local.c
test_lwt_seg6local.sh
test_maps.c
test_obj_id.c
test_offload.py
test_pkt_access.c
test_pkt_md_access.c
test_progs.c
test_select_reuseport.c
test_select_reuseport_common.h
test_select_reuseport_kern.c
test_skb_cgroup_id.sh
test_skb_cgroup_id_kern.c
test_skb_cgroup_id_user.c
test_sock.c
test_sock_addr.c
test_sock_addr.sh
test_socket_cookie.c
test_sockhash_kern.c
test_sockmap.c
test_sockmap_kern.c
test_sockmap_kern.h
test_stacktrace_build_id.c
test_stacktrace_map.c
test_tag.c
test_tcp_estats.c
test_tcpbpf.h
test_tcpbpf_kern.c
test_tcpbpf_user.c
test_tracepoint.c
test_tunnel.sh
test_tunnel_kern.c
test_verifier.c
test_verifier_log.c
test_xdp.c
test_xdp_meta.c
test_xdp_meta.sh
test_xdp_noinline.c
test_xdp_redirect.c
test_xdp_redirect.sh
trace_helpers.c
trace_helpers.h
urandom_read.c
with_addr.sh
with_tunnels.sh
breakpoints
capabilities
cgroup
cpu-hotplug
cpufreq
drivers
efivarfs
exec
filesystems
firmware
ftrace
futex
gpio
ia64
intel_pstate
ipc
kcmp
kmod
kvm
lib
locking
media_tests
membarrier
memfd
memory-hotplug
mount
mqueue
net
networking
nsfs
ntb
powerpc
prctl
proc
pstore
ptp
ptrace
rcutorture
rseq
rtc
seccomp
sigaltstack
size
sparc64
splice
static_keys
sync
sysctl
tc-testing
timers
uevent
user
vDSO
vm
watchdog
x86
zram
.gitignore
Makefile
gen_kselftest_tar.sh
kselftest.h
kselftest_harness.h
kselftest_install.sh
lib.mk
vsock
thermal
time
usb
virtio
vm
wmi
Makefile
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
tools
/
testing
/
selftests
/
bpf
/
test_socket_cookie.c
Blame
Blame
Latest commit
History
History
221 lines (179 loc) · 4.03 KB
Breadcrumbs
linux
/
tools
/
testing
/
selftests
/
bpf
/
test_socket_cookie.c
Top
File metadata and controls
Code
Blame
221 lines (179 loc) · 4.03 KB
Raw
// SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2018 Facebook #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <bpf/bpf.h> #include <bpf/libbpf.h> #include "bpf_rlimit.h" #include "cgroup_helpers.h" #define CG_PATH "/foo" #define SOCKET_COOKIE_PROG "./socket_cookie_prog.o" static int start_server(void) { struct sockaddr_in6 addr; int fd; fd = socket(AF_INET6, SOCK_STREAM, 0); if (fd == -1) { log_err("Failed to create server socket"); goto out; } memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; addr.sin6_addr = in6addr_loopback; addr.sin6_port = 0; if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) == -1) { log_err("Failed to bind server socket"); goto close_out; } if (listen(fd, 128) == -1) { log_err("Failed to listen on server socket"); goto close_out; } goto out; close_out: close(fd); fd = -1; out: return fd; } static int connect_to_server(int server_fd) { struct sockaddr_storage addr; socklen_t len = sizeof(addr); int fd; fd = socket(AF_INET6, SOCK_STREAM, 0); if (fd == -1) { log_err("Failed to create client socket"); goto out; } if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) { log_err("Failed to get server addr"); goto close_out; } if (connect(fd, (const struct sockaddr *)&addr, len) == -1) { log_err("Fail to connect to server"); goto close_out; } goto out; close_out: close(fd); fd = -1; out: return fd; } static int validate_map(struct bpf_map *map, int client_fd) { __u32 cookie_expected_value; struct sockaddr_in6 addr; socklen_t len = sizeof(addr); __u32 cookie_value; __u64 cookie_key; int err = 0; int map_fd; if (!map) { log_err("Map not found in BPF object"); goto err; } map_fd = bpf_map__fd(map); err = bpf_map_get_next_key(map_fd, NULL, &cookie_key); if (err) { log_err("Can't get cookie key from map"); goto out; } err = bpf_map_lookup_elem(map_fd, &cookie_key, &cookie_value); if (err) { log_err("Can't get cookie value from map"); goto out; } err = getsockname(client_fd, (struct sockaddr *)&addr, &len); if (err) { log_err("Can't get client local addr"); goto out; } cookie_expected_value = (ntohs(addr.sin6_port) << 8) | 0xFF; if (cookie_value != cookie_expected_value) { log_err("Unexpected value in map: %x != %x", cookie_value, cookie_expected_value); goto err; } goto out; err: err = -1; out: return err; } static int run_test(int cgfd) { enum bpf_attach_type attach_type; struct bpf_prog_load_attr attr; struct bpf_program *prog; struct bpf_object *pobj; const char *prog_name; int server_fd = -1; int client_fd = -1; int prog_fd = -1; int err = 0; memset(&attr, 0, sizeof(attr)); attr.file = SOCKET_COOKIE_PROG; attr.prog_type = BPF_PROG_TYPE_UNSPEC; err = bpf_prog_load_xattr(&attr, &pobj, &prog_fd); if (err) { log_err("Failed to load %s", attr.file); goto out; } bpf_object__for_each_program(prog, pobj) { prog_name = bpf_program__title(prog, /*needs_copy*/ false); if (libbpf_attach_type_by_name(prog_name, &attach_type)) { log_err("Unexpected prog: %s", prog_name); goto err; } err = bpf_prog_attach(bpf_program__fd(prog), cgfd, attach_type, BPF_F_ALLOW_OVERRIDE); if (err) { log_err("Failed to attach prog %s", prog_name); goto out; } } server_fd = start_server(); if (server_fd == -1) goto err; client_fd = connect_to_server(server_fd); if (client_fd == -1) goto err; if (validate_map(bpf_map__next(NULL, pobj), client_fd)) goto err; goto out; err: err = -1; out: close(client_fd); close(server_fd); bpf_object__close(pobj); printf("%s\n", err ? "FAILED" : "PASSED"); return err; } int main(int argc, char **argv) { int cgfd = -1; int err = 0; if (setup_cgroup_environment()) goto err; cgfd = create_and_get_cgroup(CG_PATH); if (!cgfd) goto err; if (join_cgroup(CG_PATH)) goto err; if (run_test(cgfd)) goto err; goto out; err: err = -1; out: close(cgfd); cleanup_cgroup_environment(); return err; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
You can’t perform that action at this time.