Skip to content

Commit 745b587

Browse files
committed
experiment01-tailgrow: add #2 example that fail xdp_prog_fail2.c
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
1 parent 54c4d33 commit 745b587

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

experiment01-tailgrow/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
XDP_TARGETS := xdp_prog_kern xdp_prog_kern2
44
XDP_TARGETS += xdp_prog_fail1
5+
XDP_TARGETS += xdp_prog_fail2
56

67
# USER_TARGETS :=
78

experiment01-tailgrow/README.org

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,16 @@ calculation cannot be used for static analysis.
5050
#+begin_src sh
5151
sudo ./xdp_loader --dev mlx5p1 --force --file xdp_prog_fail1.o
5252
#+end_src
53+
54+
** Fail#2: Use data_end directly
55+
56+
In example [[file:xdp_prog_fail2.c]], we try to use the =data_end= pointer
57+
more or less directy to find the last byte in the packet. The packet
58+
data [[https://www.mathwords.com/i/interval_notation.htm][interval]] is defined as =[data, data_end)=, meaning that the byte
59+
=data_end= is pointing is *excluded*. The example tries to access
60+
2nd-last byte (to have a code if-construct that doesn't get removed by
61+
compiler optimizations).
62+
63+
#+begin_src sh
64+
sudo ./xdp_loader --dev mlx5p1 --force --file xdp_prog_fail2.o
65+
#+end_src
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_helpers.h>
4+
5+
/*
6+
* This BPF-prog will FAIL, due to verifier rejecting it.
7+
*
8+
* General idea: Use data_end point to access last (2nd-last) byte in
9+
* packet. That is not allowed by verifier, as pointer arithmetic on
10+
* pkt_end is prohibited.
11+
*/
12+
13+
SEC("xdp_fail2")
14+
int _xdp_fail2(struct xdp_md *ctx)
15+
{
16+
void *data_end = (void *)(long)ctx->data_end;
17+
volatile unsigned char *ptr;
18+
volatile void *pos;
19+
20+
pos = data_end;
21+
22+
#pragma clang optimize off
23+
if (pos - 1 > data_end)
24+
goto out;
25+
#pragma clang optimize on
26+
27+
/* Verifier fails with: "pointer arithmetic on pkt_end prohibited"
28+
*/
29+
ptr = pos - 2;
30+
if (*ptr == 0xFF)
31+
return XDP_ABORTED;
32+
out:
33+
return XDP_PASS;
34+
}

experiment01-tailgrow/xdp_prog_kern2.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,30 +125,6 @@ int _xdp_test2(struct xdp_md *ctx)
125125
return xdp_stats_record_action(ctx, XDP_PASS);
126126
}
127127

128-
/* Also invalid
129-
SEC("xdp_test4")
130-
int _xdp_test4(struct xdp_md *ctx)
131-
{
132-
void *data_end = (void *)(long)ctx->data_end;
133-
volatile unsigned char *ptr;
134-
volatile void *pos;
135-
136-
pos = data_end;
137-
138-
#pragma clang optimize off
139-
if (pos - 1 > data_end)
140-
goto out;
141-
142-
ptr = pos - 2; //Err: "pointer arithmetic on pkt_end prohibited"
143-
if (*ptr == 0xFF)
144-
return XDP_ABORTED;
145-
#pragma clang optimize on
146-
out:
147-
return XDP_PASS;
148-
}
149-
*/
150-
151-
152128
SEC("xdp_pass")
153129
int xdp_pass_f1(struct xdp_md *ctx)
154130
{

0 commit comments

Comments
 (0)