Skip to content

Instantly share code, notes, and snippets.

@majek
Created June 17, 2022 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majek/209bfb85c0f4d6e4c7b6f2f0e369457d to your computer and use it in GitHub Desktop.
Save majek/209bfb85c0f4d6e4c7b6f2f0e369457d to your computer and use it in GitHub Desktop.
repro_access_to_stack_error_bpf
.PHONY: repro
repro:
$(CLANG) -Wall -Wextra -O2 --target=bpf -c -g \
repro.bpf.c \
-o repro.bpf.o
bpftool gen skeleton repro.bpf.o > repro.bpf.skel.h
$(CLANG) -Wall -fno-omit-frame-pointer -Wextra -O2 -g \
repro.c \
-lbpf \
-lelf \
-lz \
-o repro
#include <stddef.h>
#include <string.h>
#include <linux/types.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <linux/bpf.h>
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 4 * 1024);
} rb SEC(".maps");
struct event {
__u64 this_field_works_with_u32_fails_with_u64;
char v6[16];
};
SEC("sockops")
int bpf_testcb(struct bpf_sock_ops *skops)
{
struct event e = {};
memcpy(&e.v6, &skops->remote_ip6, 16);
bpf_ringbuf_output(&rb, &e, sizeof(e), 0);
return 1;
}
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include "repro.bpf.skel.h"
int main()
{
struct repro_bpf *skel = repro_bpf__open_and_load();
if (!skel) {
error(1, errno, "Failed to open and load BPF skeleton\n");
}
}
@majek
Copy link
Author

majek commented Jun 20, 2022

Solution:

static void u32_memcpy(void *dest, void *src, size_t n){
	__u32 *d = dest;
	__u32 *s = src;
	size_t o = 0;

	while (o < n/4) {
		d[o] = s[o];
		o += 1;
	}
	o*=4;
	if (n - o > 0) {
		__builtin_memcpy(d+o, s+o, n-o);
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment