summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-02-04 10:17:59 +0200
committerPaul Buetow <paul@buetow.org>2024-02-04 10:17:59 +0200
commit6f0b9a473ab8e3fd8ac58724e907809da36b1bd1 (patch)
tree8b60b3a6b665bcef4d00e7453000d7aca4716e5f
parent2964fe4a7185d75b84745edee5edc9dcf5fc4b4f (diff)
initial version (not yet working)
-rw-r--r--LICENSE9
-rw-r--r--Makefile87
-rw-r--r--README.md1
-rw-r--r--go.mod7
-rw-r--r--go.sum4
-rw-r--r--main.bpf.c45
-rw-r--r--main.go131
-rwxr-xr-xrun.sh22
8 files changed, 297 insertions, 9 deletions
diff --git a/LICENSE b/LICENSE
deleted file mode 100644
index 463fd9b..0000000
--- a/LICENSE
+++ /dev/null
@@ -1,9 +0,0 @@
-MIT License
-
-Copyright (c) 2024 snonux
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..00aee0a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,87 @@
+BASEDIR = $(abspath ../../)
+
+OUTPUT = ../../output
+
+LIBBPF_SRC = $(abspath ../../libbpf/src)
+LIBBPF_OBJ = $(abspath $(OUTPUT)/libbpf.a)
+
+CLANG = clang
+CC = $(CLANG)
+GO = go
+
+ARCH := $(shell uname -m | sed 's/x86_64/amd64/g; s/aarch64/arm64/g')
+
+CFLAGS = -g -O2 -Wall -fpie -I$(abspath ../common)
+LDFLAGS =
+
+CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT)) -I$(abspath ../common)"
+CGO_LDFLAGS_STATIC = "-lelf -lzstd $(LIBBPF_OBJ)"
+CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"'
+
+CGO_CFLAGS_DYN = "-I. -I/usr/include/"
+CGO_LDFLAGS_DYN = "-lelf -lz -lbpf"
+
+MAIN = main
+
+.PHONY: $(MAIN)
+.PHONY: $(MAIN).go
+.PHONY: $(MAIN).bpf.c
+
+all: $(MAIN)-static
+
+.PHONY: libbpfgo
+.PHONY: libbpfgo-static
+.PHONY: libbpfgo-dynamic
+
+## libbpfgo
+
+libbpfgo-static:
+ $(MAKE) -C $(BASEDIR) libbpfgo-static
+
+libbpfgo-dynamic:
+ $(MAKE) -C $(BASEDIR) libbpfgo-dynamic
+
+outputdir:
+ $(MAKE) -C $(BASEDIR) outputdir
+
+## test bpf dependency
+
+$(MAIN).bpf.o: $(MAIN).bpf.c
+ $(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@
+
+## test
+
+.PHONY: $(MAIN)-static
+.PHONY: $(MAIN)-dynamic
+
+$(MAIN)-static: libbpfgo-static | $(MAIN).bpf.o
+ CC=$(CLANG) \
+ CGO_CFLAGS=$(CGO_CFLAGS_STATIC) \
+ CGO_LDFLAGS=$(CGO_LDFLAGS_STATIC) \
+ GOOS=linux GOARCH=$(ARCH) \
+ $(GO) build \
+ -tags netgo -ldflags $(CGO_EXTLDFLAGS_STATIC) \
+ -o $(MAIN)-static ./$(MAIN).go
+
+$(MAIN)-dynamic: libbpfgo-dynamic | $(MAIN).bpf.o
+ CC=$(CLANG) \
+ CGO_CFLAGS=$(CGO_CFLAGS_DYN) \
+ CGO_LDFLAGS=$(CGO_LDFLAGS_DYN) \
+ $(GO) build -o ./$(MAIN)-dynamic ./$(MAIN).go
+
+## run
+
+.PHONY: run
+.PHONY: run-static
+.PHONY: run-dynamic
+
+run: run-static
+
+run-static: $(MAIN)-static
+ sudo ./run.sh $(MAIN)-static
+
+run-dynamic: $(MAIN)-dynamic
+ sudo ./run.sh $(MAIN)-dynamic
+
+clean:
+ rm -f *.o *-static *-dynamic
diff --git a/README.md b/README.md
index 758eef4..22b954a 100644
--- a/README.md
+++ b/README.md
@@ -26,6 +26,7 @@ rpmbuild -ba *.spec
mkdir ~/src
tar -xvjpf ~/rpmbuild/SOURCES/elfutils-*.tar.bz2
cd ~/src/elfutils-*
+rm -Rf ~/rpmbuild
./configure
make
sudo cp -v ./libelf/libelf.a /usr/lib64/
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..1fd0933
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,7 @@
+module github.com/aquasecurity/libbpfgo/selftest/map-update
+
+go 1.18
+
+require github.com/aquasecurity/libbpfgo v0.4.7-libbpf-1.2.0-b2e29a1
+
+replace github.com/aquasecurity/libbpfgo => ../../
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..c60af66
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,4 @@
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
diff --git a/main.bpf.c b/main.bpf.c
new file mode 100644
index 0000000..3c8a928
--- /dev/null
+++ b/main.bpf.c
@@ -0,0 +1,45 @@
+//+build ignore
+
+#include <vmlinux.h>
+
+#include <bpf/bpf_helpers.h>
+
+struct value {
+ int x;
+ char y;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __type(key, u32);
+ __type(value, struct value);
+ __uint(max_entries, 1 << 24);
+} tester SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+ __uint(key_size, sizeof(u32));
+ __uint(value_size, sizeof(u32));
+} events SEC(".maps");
+
+SEC("kprobe/sys_mmap")
+int kprobe__sys_mmap(struct pt_regs *ctx)
+{
+ u32 firstKey = 1;
+ struct value *v1 = bpf_map_lookup_elem(&tester, &firstKey);
+ if (!v1) {
+ return 1;
+ }
+ bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, v1, sizeof(struct value));
+
+ s64 secondKey = 42069420;
+ struct value *v2 = bpf_map_lookup_elem(&tester, &secondKey);
+ if (!v2) {
+ return 1;
+ }
+ bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, v2, sizeof(char) * 3);
+
+ return 0;
+}
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..23301b0
--- /dev/null
+++ b/main.go
@@ -0,0 +1,131 @@
+package main
+
+import "C"
+
+import (
+ "os"
+ "runtime"
+ "time"
+ "unsafe"
+
+ "encoding/binary"
+ "fmt"
+ "syscall"
+
+ bpf "github.com/aquasecurity/libbpfgo"
+)
+
+func resizeMap(module *bpf.Module, name string, size uint32) error {
+ m, err := module.GetMap("events")
+ if err != nil {
+ return err
+ }
+
+ if err = m.Resize(size); err != nil {
+ return err
+ }
+
+ if actual := m.GetMaxEntries(); actual != size {
+ return fmt.Errorf("map resize failed, expected %v, actual %v", size, actual)
+ }
+
+ return nil
+}
+
+func main() {
+ bpfModule, err := bpf.NewModuleFromFile("main.bpf.o")
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(-1)
+ }
+ defer bpfModule.Close()
+
+ if err = resizeMap(bpfModule, "events", 8192); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(-1)
+ }
+
+ bpfModule.BPFLoadObject()
+ prog, err := bpfModule.GetProgram("kprobe__sys_mmap")
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(-1)
+ }
+
+ testerMap, err := bpfModule.GetMap("tester")
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(-1)
+ }
+
+ if testerMap.Name() != "tester" {
+ fmt.Fprintln(os.Stderr, "wrong map")
+ os.Exit(-1)
+ }
+
+ if testerMap.Type() != bpf.MapTypeHash {
+ fmt.Fprintln(os.Stderr, "wrong map type")
+ os.Exit(-1)
+ }
+
+ key1 := uint32(1)
+ value1 := struct{ x int }{50}
+ key1Unsafe := unsafe.Pointer(&key1)
+ value1Unsafe := unsafe.Pointer(&value1)
+ testerMap.Update(key1Unsafe, value1Unsafe)
+
+ key2 := int64(42069420)
+ value2 := []byte{'a', 'b', 'c'}
+ key2Unsafe := unsafe.Pointer(&key2)
+ value2Unsafe := unsafe.Pointer(&value2[0])
+ testerMap.Update(key2Unsafe, value2Unsafe)
+
+ funcName := fmt.Sprintf("__%s_sys_mmap", ksymArch())
+ _, err = prog.AttachKprobe(funcName)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(-1)
+ }
+
+ eventsChannel := make(chan []byte)
+ lostChannel := make(chan uint64)
+ pb, err := bpfModule.InitPerfBuf("events", eventsChannel, lostChannel, 1)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(-1)
+ }
+
+ pb.Poll(300)
+
+ go func() {
+ time.Sleep(time.Second)
+ syscall.Mmap(999, 999, 999, 1, 1)
+ syscall.Mmap(999, 999, 999, 1, 1)
+ }()
+
+ ev := <-eventsChannel
+ if binary.LittleEndian.Uint32(ev) != 50 {
+ fmt.Fprintf(os.Stderr, "invalid data retrieved\n")
+ os.Exit(-1)
+ }
+
+ ev = <-eventsChannel
+ if ev[0] != value2[0] || ev[1] != value2[1] || ev[2] != value2[2] {
+ fmt.Fprintf(os.Stderr, "invalid data retrieved\n")
+ os.Exit(-1)
+ }
+
+ pb.Stop()
+ pb.Close()
+}
+
+func ksymArch() string {
+ switch runtime.GOARCH {
+ case "amd64":
+ return "x64"
+ case "arm64":
+ return "arm64"
+ default:
+ panic("unsupported architecture")
+ }
+}
diff --git a/run.sh b/run.sh
new file mode 100755
index 0000000..c39f257
--- /dev/null
+++ b/run.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+# SETTINGS
+
+TEST=$(dirname $0)/$1 # execute
+TIMEOUT=10 # seconds
+
+# COMMON
+
+COMMON="$(dirname $0)/../common/common.sh"
+[[ -f $COMMON ]] && { . $COMMON; } || { error "no common"; exit 1; }
+
+# MAIN
+
+kern_version gt 4.18
+
+check_build
+check_ppid
+test_exec
+test_finish
+
+exit 0