Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
Note: File does not exist in v6.8.
 1// SPDX-License-Identifier: GPL-2.0
 2/* Copyright (c) 2024 Isovalent */
 3
 4#include "vmlinux.h"
 5#include <bpf/bpf_helpers.h>
 6#include <bpf/bpf_tracing.h>
 7#include "bpf_misc.h"
 8
 9const volatile long foo = 42;
10long bar;
11long bart = 96;
12
13SEC("tc/ingress")
14__description("rodata/strtol: write rejected")
15__failure __msg("write into map forbidden")
16int tcx1(struct __sk_buff *skb)
17{
18	char buff[] = { '8', '4', '\0' };
19	bpf_strtol(buff, sizeof(buff), 0, (long *)&foo);
20	return TCX_PASS;
21}
22
23SEC("tc/ingress")
24__description("bss/strtol: write accepted")
25__success
26int tcx2(struct __sk_buff *skb)
27{
28	char buff[] = { '8', '4', '\0' };
29	bpf_strtol(buff, sizeof(buff), 0, &bar);
30	return TCX_PASS;
31}
32
33SEC("tc/ingress")
34__description("data/strtol: write accepted")
35__success
36int tcx3(struct __sk_buff *skb)
37{
38	char buff[] = { '8', '4', '\0' };
39	bpf_strtol(buff, sizeof(buff), 0, &bart);
40	return TCX_PASS;
41}
42
43SEC("tc/ingress")
44__description("rodata/mtu: write rejected")
45__failure __msg("write into map forbidden")
46int tcx4(struct __sk_buff *skb)
47{
48	bpf_check_mtu(skb, skb->ifindex, (__u32 *)&foo, 0, 0);
49	return TCX_PASS;
50}
51
52SEC("tc/ingress")
53__description("bss/mtu: write accepted")
54__success
55int tcx5(struct __sk_buff *skb)
56{
57	bpf_check_mtu(skb, skb->ifindex, (__u32 *)&bar, 0, 0);
58	return TCX_PASS;
59}
60
61SEC("tc/ingress")
62__description("data/mtu: write accepted")
63__success
64int tcx6(struct __sk_buff *skb)
65{
66	bpf_check_mtu(skb, skb->ifindex, (__u32 *)&bart, 0, 0);
67	return TCX_PASS;
68}
69
70static inline void write_fixed(volatile void *p, __u32 val)
71{
72	*(volatile __u32 *)p = val;
73}
74
75static inline void write_dyn(void *p, void *val, int len)
76{
77	bpf_copy_from_user(p, len, val);
78}
79
80SEC("tc/ingress")
81__description("rodata/mark: write with unknown reg rejected")
82__failure __msg("write into map forbidden")
83int tcx7(struct __sk_buff *skb)
84{
85	write_fixed((void *)&foo, skb->mark);
86	return TCX_PASS;
87}
88
89SEC("lsm.s/bprm_committed_creds")
90__description("rodata/mark: write with unknown reg rejected")
91__failure __msg("write into map forbidden")
92int BPF_PROG(bprm, struct linux_binprm *bprm)
93{
94	write_dyn((void *)&foo, &bart, bpf_get_prandom_u32() & 3);
95	return 0;
96}
97
98char LICENSE[] SEC("license") = "GPL";