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: <20200723095953.1003302-3-jakub@cloudflare.com>
Date:   Thu, 23 Jul 2020 11:59:53 +0200
From:   Jakub Sitnicki <jakub@...udflare.com>
To:     bpf@...r.kernel.org
Cc:     netdev@...r.kernel.org, kernel-team@...udflare.com,
        Alexei Starovoitov <ast@...nel.org>,
        Daniel Borkmann <daniel@...earbox.net>
Subject: [PATCH bpf v2 2/2] selftests/bpf: Add test for narrow loads from context at an offset

Check that narrow loads at various offsets from a context field backed by a
target field that is smaller in size work as expected. That is target field
value is loaded only when the offset is less than the target field size.
While for offsets beyond the target field, the loaded value is zero.

Signed-off-by: Jakub Sitnicki <jakub@...udflare.com>
---
 .../selftests/bpf/prog_tests/narrow_load.c    | 84 +++++++++++++++++++
 .../selftests/bpf/progs/test_narrow_load.c    | 43 ++++++++++
 2 files changed, 127 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/narrow_load.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_narrow_load.c

diff --git a/tools/testing/selftests/bpf/prog_tests/narrow_load.c b/tools/testing/selftests/bpf/prog_tests/narrow_load.c
new file mode 100644
index 000000000000..6d79d722a66d
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/narrow_load.c
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+// Copyright (c) 2020 Cloudflare
+
+#include "test_progs.h"
+#include "test_narrow_load.skel.h"
+
+static int duration;
+
+void run_sk_reuseport_prog(struct bpf_program *reuseport_prog)
+{
+	static const struct timeval timeo = { .tv_sec = 3 };
+	struct sockaddr_in addr = {
+		.sin_family = AF_INET,
+		.sin_port = 0,
+		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+	};
+	socklen_t len = sizeof(addr);
+	int err, fd, prog_fd;
+	const int one = 1;
+	char buf = 42;
+	ssize_t n;
+
+	prog_fd = bpf_program__fd(reuseport_prog);
+	if (CHECK(prog_fd < 0, "bpf_program__fd", "errno %d\n", errno))
+		return;
+
+	fd = socket(AF_INET, SOCK_DGRAM, 0);
+	if (CHECK(fd < 0, "socket", "errno %d\n", errno))
+		return;
+
+	/* Setup timeouts */
+	err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
+	if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "errno %d\n", errno))
+		goto out_close;
+	err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
+	if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "errno %d\n", errno))
+		goto out_close;
+
+	/* Setup reuseport prog */
+	err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
+	if (CHECK(err, "setsockopt(SO_REUSEPORT)", "errno %d\n", errno))
+		goto out_close;
+	err = setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF,
+			 &prog_fd, sizeof(prog_fd));
+	if (CHECK(err, "setsockopt(SO_ATTACH_REUEPORT_EBPF)",
+		  "errno %d\n", errno))
+		goto out_close;
+
+	err = bind(fd, (void *)&addr, len);
+	if (CHECK(err, "bind", "errno %d\n", errno))
+		goto out_close;
+	err = getsockname(fd, (void *)&addr, &len);
+	if (CHECK(err, "getsockname", "errno %d\n", errno))
+		goto out_close;
+
+	/* Send a message to itself to trigger reuseport prog */
+	n = sendto(fd, &buf, sizeof(buf), 0, (void *)&addr, len);
+	if (CHECK(n < 1, "sendto", "ret %ld errno %d\n", n, errno))
+		goto out_close;
+	n = recv(fd, &buf, sizeof(buf), 0);
+	if (CHECK(n < 1, "recv", "ret %ld errno %d\n", n, errno))
+		goto out_close;
+
+	/* Pass, reuseport prog didn't drop the packet */
+
+out_close:
+	close(fd);
+}
+
+void test_narrow_load(void)
+{
+	struct test_narrow_load *skel;
+
+	skel = test_narrow_load__open_and_load();
+	if (CHECK(!skel, "skel open_and_load", "failed\n"))
+		return;
+
+	if (test__start_subtest("narrow load byte"))
+		run_sk_reuseport_prog(skel->progs.narrow_load_byte);
+	if (test__start_subtest("narrow load half word"))
+		run_sk_reuseport_prog(skel->progs.narrow_load_half_word);
+
+	test_narrow_load__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_narrow_load.c b/tools/testing/selftests/bpf/progs/test_narrow_load.c
new file mode 100644
index 000000000000..57444720df16
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_narrow_load.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+// Copyright (c) 2020 Cloudflare
+
+#include <stdint.h>
+
+#include <linux/bpf.h>
+#include <linux/in.h>
+
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+/* Check 1-byte load from 2-byte wide target field */
+SEC("sk_reuseport/narrow_load_byte")
+int narrow_load_byte(struct sk_reuseport_md *ctx)
+{
+	__u8 *byte;
+
+	byte = (__u8 *)&ctx->ip_protocol;
+	if (byte[0] != IPPROTO_UDP)
+		return SK_DROP;
+	if (byte[1] != 0)
+		return SK_DROP;
+	if (byte[2] != 0)
+		return SK_DROP;
+	if (byte[3] != 0)
+		return SK_DROP;
+	return SK_PASS;
+}
+
+/* Check 2-byte load from 2-byte wide target field */
+SEC("sk_reuseport/narrow_load_half_word")
+int narrow_load_half_word(struct sk_reuseport_md *ctx)
+{
+	__u16 *half;
+
+	half = (__u16 *)&ctx->ip_protocol;
+	if (half[0] != IPPROTO_UDP)
+		return SK_DROP;
+	if (half[1] != 0)
+		return SK_DROP;
+	return SK_PASS;
+}
-- 
2.25.4

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ