Commit 81374aaa authored by John Fastabend's avatar John Fastabend Committed by David S. Miller

bpf: harden sockmap program attach to ensure correct map type

When attaching a program to sockmap we need to check map type
is correct.

Fixes: 174a79ff ("bpf: sockmap with sk redirect support")
Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent ed85054d
...@@ -794,6 +794,9 @@ int sock_map_attach_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type) ...@@ -794,6 +794,9 @@ int sock_map_attach_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type)
struct bpf_stab *stab = container_of(map, struct bpf_stab, map); struct bpf_stab *stab = container_of(map, struct bpf_stab, map);
struct bpf_prog *orig; struct bpf_prog *orig;
if (unlikely(map->map_type != BPF_MAP_TYPE_SOCKMAP))
return -EINVAL;
switch (type) { switch (type) {
case BPF_SK_SKB_STREAM_PARSER: case BPF_SK_SKB_STREAM_PARSER:
orig = xchg(&stab->bpf_parse, prog); orig = xchg(&stab->bpf_parse, prog);
......
...@@ -26,6 +26,13 @@ struct bpf_map_def SEC("maps") sock_map_tx = { ...@@ -26,6 +26,13 @@ struct bpf_map_def SEC("maps") sock_map_tx = {
.max_entries = 20, .max_entries = 20,
}; };
struct bpf_map_def SEC("maps") sock_map_break = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 20,
};
SEC("sk_skb2") SEC("sk_skb2")
int bpf_prog2(struct __sk_buff *skb) int bpf_prog2(struct __sk_buff *skb)
{ {
......
...@@ -463,12 +463,12 @@ static void test_devmap(int task, void *data) ...@@ -463,12 +463,12 @@ static void test_devmap(int task, void *data)
#define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o" #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
static void test_sockmap(int task, void *data) static void test_sockmap(int task, void *data)
{ {
int one = 1, map_fd_rx, map_fd_tx, map_fd_break, s, sc, rc;
struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_break;
int ports[] = {50200, 50201, 50202, 50204}; int ports[] = {50200, 50201, 50202, 50204};
int err, i, fd, sfd[6] = {0xdeadbeef}; int err, i, fd, sfd[6] = {0xdeadbeef};
u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0}; u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
int one = 1, map_fd_rx, map_fd_tx, s, sc, rc;
int parse_prog, verdict_prog; int parse_prog, verdict_prog;
struct bpf_map *bpf_map_rx, *bpf_map_tx;
struct sockaddr_in addr; struct sockaddr_in addr;
struct bpf_object *obj; struct bpf_object *obj;
struct timeval to; struct timeval to;
...@@ -609,17 +609,36 @@ static void test_sockmap(int task, void *data) ...@@ -609,17 +609,36 @@ static void test_sockmap(int task, void *data)
goto out_sockmap; goto out_sockmap;
} }
bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
if (IS_ERR(bpf_map_break)) {
printf("Failed to load map tx from verdict prog\n");
goto out_sockmap;
}
map_fd_break = bpf_map__fd(bpf_map_break);
if (map_fd_break < 0) {
printf("Failed to get map tx fd\n");
goto out_sockmap;
}
err = bpf_prog_attach(parse_prog, map_fd_break,
BPF_SK_SKB_STREAM_PARSER, 0);
if (!err) {
printf("Allowed attaching SK_SKB program to invalid map\n");
goto out_sockmap;
}
err = bpf_prog_attach(parse_prog, map_fd_rx, err = bpf_prog_attach(parse_prog, map_fd_rx,
BPF_SK_SKB_STREAM_PARSER, 0); BPF_SK_SKB_STREAM_PARSER, 0);
if (err) { if (err) {
printf("Failed bpf prog attach\n"); printf("Failed stream parser bpf prog attach\n");
goto out_sockmap; goto out_sockmap;
} }
err = bpf_prog_attach(verdict_prog, map_fd_rx, err = bpf_prog_attach(verdict_prog, map_fd_rx,
BPF_SK_SKB_STREAM_VERDICT, 0); BPF_SK_SKB_STREAM_VERDICT, 0);
if (err) { if (err) {
printf("Failed bpf prog attach\n"); printf("Failed stream verdict bpf prog attach\n");
goto out_sockmap; goto out_sockmap;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment