bpf: add sk_msg prog sk access tests to test_verifier
authorJohn Fastabend <john.fastabend@gmail.com>
Thu, 17 May 2018 21:17:03 +0000 (14:17 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Fri, 18 May 2018 20:44:11 +0000 (22:44 +0200)
Add tests for BPF_PROG_TYPE_SK_MSG to test_verifier for read access
to new sk fields.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/include/uapi/linux/bpf.h
tools/testing/selftests/bpf/test_verifier.c

index d94d333a82259cbf07f6725445f7768b96a24419..97446bbe2ca52699b6cc11090b4093c35da77348 100644 (file)
@@ -2176,6 +2176,14 @@ enum sk_action {
 struct sk_msg_md {
        void *data;
        void *data_end;
+
+       __u32 family;
+       __u32 remote_ip4;       /* Stored in network byte order */
+       __u32 local_ip4;        /* Stored in network byte order */
+       __u32 remote_ip6[4];    /* Stored in network byte order */
+       __u32 local_ip6[4];     /* Stored in network byte order */
+       __u32 remote_port;      /* Stored in network byte order */
+       __u32 local_port;       /* stored in host byte order */
 };
 
 #define BPF_TAG_SIZE   8
index a877af00605d199a44c9e48226a840e4359001de..6ec4d9def877f684b10adba037bc68f5db1642f8 100644 (file)
@@ -1685,6 +1685,121 @@ static struct bpf_test tests[] = {
                .result = ACCEPT,
                .prog_type = BPF_PROG_TYPE_SK_SKB,
        },
+       {
+               "valid access family in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, family)),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SK_MSG,
+       },
+       {
+               "valid access remote_ip4 in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, remote_ip4)),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SK_MSG,
+       },
+       {
+               "valid access local_ip4 in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, local_ip4)),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SK_MSG,
+       },
+       {
+               "valid access remote_port in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, remote_port)),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SK_MSG,
+       },
+       {
+               "valid access local_port in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, local_port)),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SK_MSG,
+       },
+       {
+               "valid access remote_ip6 in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, remote_ip6[0])),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, remote_ip6[1])),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, remote_ip6[2])),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, remote_ip6[3])),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SK_SKB,
+       },
+       {
+               "valid access local_ip6 in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, local_ip6[0])),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, local_ip6[1])),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, local_ip6[2])),
+                       BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, local_ip6[3])),
+                       BPF_EXIT_INSN(),
+               },
+               .result = ACCEPT,
+               .prog_type = BPF_PROG_TYPE_SK_SKB,
+       },
+       {
+               "invalid 64B read of family in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_DW, BPF_REG_2, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, family)),
+                       BPF_EXIT_INSN(),
+               },
+               .errstr = "invalid bpf_context access",
+               .result = REJECT,
+               .prog_type = BPF_PROG_TYPE_SK_MSG,
+       },
+       {
+               "invalid read past end of SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, local_port) + 4),
+                       BPF_EXIT_INSN(),
+               },
+               .errstr = "R0 !read_ok",
+               .result = REJECT,
+               .prog_type = BPF_PROG_TYPE_SK_MSG,
+       },
+       {
+               "invalid read offset in SK_MSG",
+               .insns = {
+                       BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
+                                   offsetof(struct sk_msg_md, family) + 1),
+                       BPF_EXIT_INSN(),
+               },
+               .errstr = "invalid bpf_context access",
+               .result = REJECT,
+               .prog_type = BPF_PROG_TYPE_SK_MSG,
+       },
        {
                "direct packet read for SK_MSG",
                .insns = {