-
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.
Udip Pant says: ==================== This patch series adds changes in verifier to make decisions such as granting of read / write access or enforcement of return code status based on the program type of the target program while using dynamic program extension (of type BPF_PROG_TYPE_EXT). The BPF_PROG_TYPE_EXT type can be used to extend types such as XDP, SKB and others. Since the BPF_PROG_TYPE_EXT program type on itself is just a placeholder for those, we need this extended check for those extended programs to actually work with proper access, while using this option. Patch #1 includes changes in the verifier. Patch #2 adds selftests to verify write access on a packet for a valid extension program type Patch #3 adds selftests to verify proper check for the return code Patch #4 adds selftests to ensure access permissions and restrictions for some map types such sockmap. Changelogs: v2 -> v3: * more comprehensive resolution of the program type in the verifier based on the target program (and not just for the packet access) * selftests for checking return code and map access * Also moved this patch to 'bpf-next' from 'bpf' tree v1 -> v2: * extraction of the logic to resolve prog type into a separate method * selftests to check for packet access for a valid freplace prog ==================== Acked-by: Yonghong Song <yhs@fb.com> Acked-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
- Loading branch information
Showing
7 changed files
with
229 additions
and
11 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
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,40 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2020 Facebook | ||
|
||
#include <linux/ptrace.h> | ||
#include <linux/bpf.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
#define VAR_NUM 2 | ||
|
||
struct hmap_elem { | ||
struct bpf_spin_lock lock; | ||
int var[VAR_NUM]; | ||
}; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_HASH); | ||
__uint(max_entries, 1); | ||
__type(key, __u32); | ||
__type(value, struct hmap_elem); | ||
} hash_map SEC(".maps"); | ||
|
||
SEC("freplace/handle_kprobe") | ||
int new_handle_kprobe(struct pt_regs *ctx) | ||
{ | ||
struct hmap_elem zero = {}, *val; | ||
int key = 0; | ||
|
||
val = bpf_map_lookup_elem(&hash_map, &key); | ||
if (!val) | ||
return 1; | ||
/* spin_lock in hash map */ | ||
bpf_spin_lock(&val->lock); | ||
val->var[0] = 99; | ||
bpf_spin_unlock(&val->lock); | ||
|
||
return 0; | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2020 Facebook | ||
|
||
#include <linux/stddef.h> | ||
#include <linux/bpf.h> | ||
#include <linux/pkt_cls.h> | ||
#include <bpf/bpf_endian.h> | ||
#include <bpf/bpf_helpers.h> | ||
|
||
struct bpf_map_def SEC("maps") sock_map = { | ||
.type = BPF_MAP_TYPE_SOCKMAP, | ||
.key_size = sizeof(int), | ||
.value_size = sizeof(int), | ||
.max_entries = 2, | ||
}; | ||
|
||
SEC("freplace/cls_redirect") | ||
int freplace_cls_redirect_test(struct __sk_buff *skb) | ||
{ | ||
int ret = 0; | ||
const int zero = 0; | ||
struct bpf_sock *sk; | ||
|
||
sk = bpf_map_lookup_elem(&sock_map, &zero); | ||
if (!sk) | ||
return TC_ACT_SHOT; | ||
|
||
ret = bpf_map_update_elem(&sock_map, &zero, sk, 0); | ||
bpf_sk_release(sk); | ||
|
||
return ret == 0 ? TC_ACT_OK : TC_ACT_SHOT; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |
19 changes: 19 additions & 0 deletions
19
tools/testing/selftests/bpf/progs/freplace_connect_v4_prog.c
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,19 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
// Copyright (c) 2020 Facebook | ||
|
||
#include <linux/stddef.h> | ||
#include <linux/ipv6.h> | ||
#include <linux/bpf.h> | ||
#include <linux/in.h> | ||
#include <sys/socket.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_endian.h> | ||
|
||
SEC("freplace/connect_v4_prog") | ||
int new_connect_v4_prog(struct bpf_sock_addr *ctx) | ||
{ | ||
// return value thats in invalid range | ||
return 255; | ||
} | ||
|
||
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