summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/test_bpf_nf_fail.c
blob: 2c156cd166afe7a6ea02e0fc0ad5c148b1f1bb0e (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
// SPDX-License-Identifier: GPL-2.0
#define BPF_NO_KFUNC_PROTOTYPES
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "bpf_misc.h"

struct nf_conn;

struct bpf_ct_opts___local {
	s32 netns_id;
	s32 error;
	u8 l4proto;
	u8 reserved[3];
} __attribute__((preserve_access_index));

struct nf_conn *bpf_skb_ct_alloc(struct __sk_buff *, struct bpf_sock_tuple *, u32,
				 struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
				  struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_xdp_ct_alloc(struct xdp_md *, struct bpf_sock_tuple *, u32,
				 struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32,
				  struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_ct_insert_entry(struct nf_conn *) __ksym;
void bpf_ct_release(struct nf_conn *) __ksym;
void bpf_ct_set_timeout(struct nf_conn *, u32) __ksym;
int bpf_ct_change_timeout(struct nf_conn *, u32) __ksym;
int bpf_ct_set_status(struct nf_conn *, u32) __ksym;
int bpf_ct_change_status(struct nf_conn *, u32) __ksym;

SEC("?tc")
int alloc_release(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
	if (!ct)
		return 0;
	bpf_ct_release(ct);
	return 0;
}

SEC("?tc")
int insert_insert(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
	if (!ct)
		return 0;
	ct = bpf_ct_insert_entry(ct);
	if (!ct)
		return 0;
	ct = bpf_ct_insert_entry(ct);
	return 0;
}

SEC("?tc")
int lookup_insert(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_lookup(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
	if (!ct)
		return 0;
	bpf_ct_insert_entry(ct);
	return 0;
}

SEC("?tc")
int write_not_allowlisted_field(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_lookup(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
	if (!ct)
		return 0;
	ct->status = 0xF00;
	return 0;
}

SEC("?tc")
int set_timeout_after_insert(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
	if (!ct)
		return 0;
	ct = bpf_ct_insert_entry(ct);
	if (!ct)
		return 0;
	bpf_ct_set_timeout(ct, 0);
	return 0;
}

SEC("?tc")
int set_status_after_insert(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
	if (!ct)
		return 0;
	ct = bpf_ct_insert_entry(ct);
	if (!ct)
		return 0;
	bpf_ct_set_status(ct, 0);
	return 0;
}

SEC("?tc")
int change_timeout_after_alloc(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
	if (!ct)
		return 0;
	bpf_ct_change_timeout(ct, 0);
	return 0;
}

SEC("?tc")
int change_status_after_alloc(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_alloc(ctx, &tup, sizeof(tup.ipv4), &opts, sizeof(opts));
	if (!ct)
		return 0;
	bpf_ct_change_status(ct, 0);
	return 0;
}

SEC("?tc")
__failure __msg("Possibly NULL pointer passed to trusted arg1")
int lookup_null_bpf_tuple(struct __sk_buff *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_lookup(ctx, NULL, 0, &opts, sizeof(opts));
	if (ct)
		bpf_ct_release(ct);
	return 0;
}

SEC("?tc")
__failure __msg("Possibly NULL pointer passed to trusted arg3")
int lookup_null_bpf_opts(struct __sk_buff *ctx)
{
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_skb_ct_lookup(ctx, &tup, sizeof(tup.ipv4), NULL, sizeof(struct bpf_ct_opts___local));
	if (ct)
		bpf_ct_release(ct);
	return 0;
}

SEC("?xdp")
__failure __msg("Possibly NULL pointer passed to trusted arg1")
int xdp_lookup_null_bpf_tuple(struct xdp_md *ctx)
{
	struct bpf_ct_opts___local opts = {};
	struct nf_conn *ct;

	ct = bpf_xdp_ct_lookup(ctx, NULL, 0, &opts, sizeof(opts));
	if (ct)
		bpf_ct_release(ct);
	return 0;
}

SEC("?xdp")
__failure __msg("Possibly NULL pointer passed to trusted arg3")
int xdp_lookup_null_bpf_opts(struct xdp_md *ctx)
{
	struct bpf_sock_tuple tup = {};
	struct nf_conn *ct;

	ct = bpf_xdp_ct_lookup(ctx, &tup, sizeof(tup.ipv4), NULL, sizeof(struct bpf_ct_opts___local));
	if (ct)
		bpf_ct_release(ct);
	return 0;
}

char _license[] SEC("license") = "GPL";