forked from torvalds/linux
-
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.
Merge branch 'bpf-ptrs-beyond-pkt-end'
Alexei Starovoitov says: ==================== v1->v2: - removed set-but-unused variable. - added Jiri's Tested-by. In some cases LLVM uses the knowledge that branch is taken to optimze the code which causes the verifier to reject valid programs. Teach the verifier to recognize that r1 = skb->data; r1 += 10; r2 = skb->data_end; if (r1 > r2) { here r1 points beyond packet_end and subsequent if (r1 > r2) // always evaluates to "true". } ==================== Signed-off-by: Daniel Borkmann <[email protected]>
- Loading branch information
Showing
5 changed files
with
245 additions
and
23 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
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,41 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Facebook */ | ||
#include <test_progs.h> | ||
#include <network_helpers.h> | ||
#include "skb_pkt_end.skel.h" | ||
|
||
static int sanity_run(struct bpf_program *prog) | ||
{ | ||
__u32 duration, retval; | ||
int err, prog_fd; | ||
|
||
prog_fd = bpf_program__fd(prog); | ||
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), | ||
NULL, NULL, &retval, &duration); | ||
if (CHECK(err || retval != 123, "test_run", | ||
"err %d errno %d retval %d duration %d\n", | ||
err, errno, retval, duration)) | ||
return -1; | ||
return 0; | ||
} | ||
|
||
void test_test_skb_pkt_end(void) | ||
{ | ||
struct skb_pkt_end *skb_pkt_end_skel = NULL; | ||
__u32 duration = 0; | ||
int err; | ||
|
||
skb_pkt_end_skel = skb_pkt_end__open_and_load(); | ||
if (CHECK(!skb_pkt_end_skel, "skb_pkt_end_skel_load", "skb_pkt_end skeleton failed\n")) | ||
goto cleanup; | ||
|
||
err = skb_pkt_end__attach(skb_pkt_end_skel); | ||
if (CHECK(err, "skb_pkt_end_attach", "skb_pkt_end attach failed: %d\n", err)) | ||
goto cleanup; | ||
|
||
if (sanity_run(skb_pkt_end_skel->progs.main_prog)) | ||
goto cleanup; | ||
|
||
cleanup: | ||
skb_pkt_end__destroy(skb_pkt_end_skel); | ||
} |
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,54 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#define BPF_NO_PRESERVE_ACCESS_INDEX | ||
#include <vmlinux.h> | ||
#include <bpf/bpf_core_read.h> | ||
#include <bpf/bpf_helpers.h> | ||
|
||
#define NULL 0 | ||
#define INLINE __always_inline | ||
|
||
#define skb_shorter(skb, len) ((void *)(long)(skb)->data + (len) > (void *)(long)skb->data_end) | ||
|
||
#define ETH_IPV4_TCP_SIZE (14 + sizeof(struct iphdr) + sizeof(struct tcphdr)) | ||
|
||
static INLINE struct iphdr *get_iphdr(struct __sk_buff *skb) | ||
{ | ||
struct iphdr *ip = NULL; | ||
struct ethhdr *eth; | ||
|
||
if (skb_shorter(skb, ETH_IPV4_TCP_SIZE)) | ||
goto out; | ||
|
||
eth = (void *)(long)skb->data; | ||
ip = (void *)(eth + 1); | ||
|
||
out: | ||
return ip; | ||
} | ||
|
||
SEC("classifier/cls") | ||
int main_prog(struct __sk_buff *skb) | ||
{ | ||
struct iphdr *ip = NULL; | ||
struct tcphdr *tcp; | ||
__u8 proto = 0; | ||
|
||
if (!(ip = get_iphdr(skb))) | ||
goto out; | ||
|
||
proto = ip->protocol; | ||
|
||
if (proto != IPPROTO_TCP) | ||
goto out; | ||
|
||
tcp = (void*)(ip + 1); | ||
if (tcp->dest != 0) | ||
goto out; | ||
if (!tcp) | ||
goto out; | ||
|
||
return tcp->urg_ptr; | ||
out: | ||
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