lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAPhsuW7KmO0ErC3_KySMzag1bnbJAEAmXgOQS9jd8-scFai=tg@mail.gmail.com>
Date:   Wed, 18 Aug 2021 10:48:15 -0700
From:   Song Liu <song@...nel.org>
To:     Xu Liu <liuxu623@...il.com>
Cc:     Alexei Starovoitov <ast@...nel.org>,
        Daniel Borkmann <daniel@...earbox.net>,
        Andrii Nakryiko <andrii@...nel.org>,
        Martin KaFai Lau <kafai@...com>,
        Song Liu <songliubraving@...com>, Yonghong Song <yhs@...com>,
        John Fastabend <john.fastabend@...il.com>,
        KP Singh <kpsingh@...nel.org>,
        "David S . Miller" <davem@...emloft.net>,
        Jakub Kicinski <kuba@...nel.org>,
        Networking <netdev@...r.kernel.org>, bpf <bpf@...r.kernel.org>,
        open list <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH bpf-next v2 2/2] selftests/bpf: Test for get_netns_cookie

On Wed, Aug 18, 2021 at 4:00 AM Xu Liu <liuxu623@...il.com> wrote:
>
> Add test to use get_netns_cookie() from BPF_PROG_TYPE_SOCK_OPS.
>
> Signed-off-by: Xu Liu <liuxu623@...il.com>

Acked-by: Song Liu <songliubraving@...com>

> ---
>  .../selftests/bpf/prog_tests/netns_cookie.c   | 61 +++++++++++++++++++
>  .../selftests/bpf/progs/netns_cookie_prog.c   | 39 ++++++++++++
>  2 files changed, 100 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/netns_cookie.c
>  create mode 100644 tools/testing/selftests/bpf/progs/netns_cookie_prog.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/netns_cookie.c b/tools/testing/selftests/bpf/prog_tests/netns_cookie.c
> new file mode 100644
> index 000000000000..6f3cd472fb65
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/netns_cookie.c
> @@ -0,0 +1,61 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <test_progs.h>
> +#include "netns_cookie_prog.skel.h"
> +#include "network_helpers.h"
> +
> +#ifndef SO_NETNS_COOKIE
> +#define SO_NETNS_COOKIE 71
> +#endif
> +
> +static int duration;
> +
> +void test_netns_cookie(void)
> +{
> +       int server_fd = 0, client_fd = 0, cgroup_fd = 0, err = 0, val = 0;
> +       struct netns_cookie_prog *skel;
> +       uint64_t cookie_expected_value;
> +       socklen_t vallen = sizeof(cookie_expected_value);
> +
> +       skel = netns_cookie_prog__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "skel_open"))
> +               return;
> +
> +       cgroup_fd = test__join_cgroup("/netns_cookie");
> +       if (CHECK(cgroup_fd < 0, "join_cgroup", "cgroup creation failed\n"))
> +               goto out;
> +
> +       skel->links.get_netns_cookie_sockops = bpf_program__attach_cgroup(
> +               skel->progs.get_netns_cookie_sockops, cgroup_fd);
> +       if (!ASSERT_OK_PTR(skel->links.get_netns_cookie_sockops, "prog_attach"))
> +               goto close_cgroup_fd;
> +
> +       server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
> +       if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
> +               goto close_cgroup_fd;
> +
> +       client_fd = connect_to_fd(server_fd, 0);
> +       if (CHECK(client_fd < 0, "connect_to_fd", "errno %d\n", errno))
> +               goto close_server_fd;
> +
> +       err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.netns_cookies),
> +                               &client_fd, &val);
> +       if (!ASSERT_OK(err, "map_lookup(socket_cookies)"))
> +               goto close_client_fd;
> +
> +       err = getsockopt(client_fd, SOL_SOCKET, SO_NETNS_COOKIE,
> +                               &cookie_expected_value, &vallen);
> +       if (!ASSERT_OK(err, "getsockopt)"))
> +               goto close_client_fd;
> +
> +       ASSERT_EQ(val, cookie_expected_value, "cookie_value");
> +
> +close_client_fd:
> +       close(client_fd);
> +close_server_fd:
> +       close(server_fd);
> +close_cgroup_fd:
> +       close(cgroup_fd);
> +out:
> +       netns_cookie_prog__destroy(skel);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/netns_cookie_prog.c b/tools/testing/selftests/bpf/progs/netns_cookie_prog.c
> new file mode 100644
> index 000000000000..4ed8d75aa299
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/netns_cookie_prog.c
> @@ -0,0 +1,39 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include "vmlinux.h"
> +
> +#include <bpf/bpf_helpers.h>
> +
> +#define AF_INET6 10
> +
> +struct {
> +       __uint(type, BPF_MAP_TYPE_SK_STORAGE);
> +       __uint(map_flags, BPF_F_NO_PREALLOC);
> +       __type(key, int);
> +       __type(value, int);
> +} netns_cookies SEC(".maps");
> +
> +SEC("sockops")
> +int get_netns_cookie_sockops(struct bpf_sock_ops *ctx)
> +{
> +       struct bpf_sock *sk = ctx->sk;
> +       int *cookie;
> +
> +       if (ctx->family != AF_INET6)
> +               return 1;
> +
> +       if (ctx->op != BPF_SOCK_OPS_TCP_CONNECT_CB)
> +               return 1;
> +
> +       if (!sk)
> +               return 1;
> +
> +       cookie = bpf_sk_storage_get(&netns_cookies, sk, 0,
> +                               BPF_SK_STORAGE_GET_F_CREATE);
> +       if (!cookie)
> +               return 1;
> +
> +       *cookie = bpf_get_netns_cookie(ctx);
> +
> +       return 1;
> +}
> --
> 2.28.0
>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ