cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

bpf_iter_setsockopt_unix.c (2546B)


      1// SPDX-License-Identifier: GPL-2.0
      2/* Copyright Amazon.com Inc. or its affiliates. */
      3#include <sys/socket.h>
      4#include <sys/un.h>
      5#include <test_progs.h>
      6#include "bpf_iter_setsockopt_unix.skel.h"
      7
      8#define NR_CASES 5
      9
     10static int create_unix_socket(struct bpf_iter_setsockopt_unix *skel)
     11{
     12	struct sockaddr_un addr = {
     13		.sun_family = AF_UNIX,
     14		.sun_path = "",
     15	};
     16	socklen_t len;
     17	int fd, err;
     18
     19	fd = socket(AF_UNIX, SOCK_STREAM, 0);
     20	if (!ASSERT_NEQ(fd, -1, "socket"))
     21		return -1;
     22
     23	len = offsetof(struct sockaddr_un, sun_path);
     24	err = bind(fd, (struct sockaddr *)&addr, len);
     25	if (!ASSERT_OK(err, "bind"))
     26		return -1;
     27
     28	len = sizeof(addr);
     29	err = getsockname(fd, (struct sockaddr *)&addr, &len);
     30	if (!ASSERT_OK(err, "getsockname"))
     31		return -1;
     32
     33	memcpy(&skel->bss->sun_path, &addr.sun_path,
     34	       len - offsetof(struct sockaddr_un, sun_path));
     35
     36	return fd;
     37}
     38
     39static void test_sndbuf(struct bpf_iter_setsockopt_unix *skel, int fd)
     40{
     41	socklen_t optlen;
     42	int i, err;
     43
     44	for (i = 0; i < NR_CASES; i++) {
     45		if (!ASSERT_NEQ(skel->data->sndbuf_getsockopt[i], -1,
     46				"bpf_(get|set)sockopt"))
     47			return;
     48
     49		err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
     50				 &(skel->data->sndbuf_setsockopt[i]),
     51				 sizeof(skel->data->sndbuf_setsockopt[i]));
     52		if (!ASSERT_OK(err, "setsockopt"))
     53			return;
     54
     55		optlen = sizeof(skel->bss->sndbuf_getsockopt_expected[i]);
     56		err = getsockopt(fd, SOL_SOCKET, SO_SNDBUF,
     57				 &(skel->bss->sndbuf_getsockopt_expected[i]),
     58				 &optlen);
     59		if (!ASSERT_OK(err, "getsockopt"))
     60			return;
     61
     62		if (!ASSERT_EQ(skel->data->sndbuf_getsockopt[i],
     63			       skel->bss->sndbuf_getsockopt_expected[i],
     64			       "bpf_(get|set)sockopt"))
     65			return;
     66	}
     67}
     68
     69void test_bpf_iter_setsockopt_unix(void)
     70{
     71	struct bpf_iter_setsockopt_unix *skel;
     72	int err, unix_fd, iter_fd;
     73	char buf;
     74
     75	skel = bpf_iter_setsockopt_unix__open_and_load();
     76	if (!ASSERT_OK_PTR(skel, "open_and_load"))
     77		return;
     78
     79	unix_fd = create_unix_socket(skel);
     80	if (!ASSERT_NEQ(unix_fd, -1, "create_unix_server"))
     81		goto destroy;
     82
     83	skel->links.change_sndbuf = bpf_program__attach_iter(skel->progs.change_sndbuf, NULL);
     84	if (!ASSERT_OK_PTR(skel->links.change_sndbuf, "bpf_program__attach_iter"))
     85		goto destroy;
     86
     87	iter_fd = bpf_iter_create(bpf_link__fd(skel->links.change_sndbuf));
     88	if (!ASSERT_GE(iter_fd, 0, "bpf_iter_create"))
     89		goto destroy;
     90
     91	while ((err = read(iter_fd, &buf, sizeof(buf))) == -1 &&
     92	       errno == EAGAIN)
     93		;
     94	if (!ASSERT_OK(err, "read iter error"))
     95		goto destroy;
     96
     97	test_sndbuf(skel, unix_fd);
     98destroy:
     99	bpf_iter_setsockopt_unix__destroy(skel);
    100}