Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InitGlobalVariable to support setting global variables in bpf code #258

Merged
merged 8 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,15 @@ func (b *BPFMap) GetValueReadInto(key unsafe.Pointer, value *[]byte) error {
return nil
}

func (b *BPFMap) SetInitialValue(value unsafe.Pointer) error {
mozillazg marked this conversation as resolved.
Show resolved Hide resolved
sz := b.ValueSize()
ret := C.bpf_map__set_initial_value(b.bpfMap, value, C.ulong(sz))
mozillazg marked this conversation as resolved.
Show resolved Hide resolved
if ret != 0 {
return fmt.Errorf("failed to set inital value for map %s: %w", b.name, syscall.Errno(-ret))
}
return nil
}

// BPFMapBatchOpts mirrors the C structure bpf_map_batch_opts.
type BPFMapBatchOpts struct {
Sz uint64
Expand Down
1 change: 1 addition & 0 deletions selftest/global-variable/Makefile
9 changes: 9 additions & 0 deletions selftest/global-variable/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/aquasecurity/libbpfgo/selftest/global-variable

go 1.18

require github.com/aquasecurity/libbpfgo v0.2.1-libbpf-0.4.0

require golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect

replace github.com/aquasecurity/libbpfgo => ../../
12 changes: 12 additions & 0 deletions selftest/global-variable/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
42 changes: 42 additions & 0 deletions selftest/global-variable/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//+build ignore
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#ifdef asm_inline
#undef asm_inline
#define asm_inline asm
#endif

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 24);
} events SEC(".maps");

const volatile int foobar SEC(".rodata") = 0;
const volatile int foo SEC(".rodata.foo") = 0;
const volatile int bar SEC(".rodata.bar") = 0;
const volatile int baz SEC(".data.baz") = 0;
const volatile int qux SEC(".data.qux") = 0;

long ringbuffer_flags = 0;

SEC("kprobe/sys_mmap")
int kprobe__sys_mmap(struct pt_regs *ctx)
{
int *process;

// Reserve space on the ringbuffer for the sample
process = bpf_ringbuf_reserve(&events, sizeof(int), ringbuffer_flags);
if (!process) {
return 1;
}

*process = foobar + foo + bar + baz + qux;

bpf_ringbuf_submit(process, ringbuffer_flags);
return 1;
}

char LICENSE[] SEC("license") = "GPL";

77 changes: 77 additions & 0 deletions selftest/global-variable/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import "C"
import (
"encoding/binary"
"fmt"
"os"
"syscall"
"time"
"unsafe"

bpf "github.com/aquasecurity/libbpfgo"
)

func exitWithErr(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

func initGlobalVariable(bpfModule *bpf.Module, name string, value int) {
bmap, err := bpfModule.GetMap(name)
if err != nil {
exitWithErr(err)
}
val := C.int(value)
if err := bmap.SetInitialValue(unsafe.Pointer(&val)); err != nil {
exitWithErr(err)
}
}

func main() {
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o")
if err != nil {
exitWithErr(err)
}
defer bpfModule.Close()

initGlobalVariable(bpfModule, ".rodata", 2000)
initGlobalVariable(bpfModule, ".rodata.foo", 10)
initGlobalVariable(bpfModule, ".rodata.bar", 8)
initGlobalVariable(bpfModule, ".data.baz", 2)
initGlobalVariable(bpfModule, ".data.qux", 1)

if err := bpfModule.BPFLoadObject(); err != nil {
exitWithErr(err)
}

prog, err := bpfModule.GetProgram("kprobe__sys_mmap")
if err != nil {
exitWithErr(err)
}

if _, err := prog.AttachKprobe("__x64_sys_mmap"); err != nil {
exitWithErr(err)
}

eventsChannel := make(chan []byte)
rb, err := bpfModule.InitRingBuf("events", eventsChannel)
if err != nil {
exitWithErr(err)
}

rb.Start()
go func() {
time.Sleep(time.Second)
syscall.Mmap(999, 999, 999, 1, 1)
}()

b := <-eventsChannel
if binary.LittleEndian.Uint32(b) != 2021 {
fmt.Fprintf(os.Stderr, "invalid data retrieved: %v\n", b)
os.Exit(-1)
}

rb.Stop()
rb.Close()
}
1 change: 1 addition & 0 deletions selftest/global-variable/run.sh