Skip to content

Commit 78e0433

Browse files
deansheatherKatie Horne
and
Katie Horne
authored
chore: rename import path, fix gid bug, fix license (#4)
Co-authored-by: Kyle Carberry Co-authored-by: Katie Horne <[email protected]>
1 parent 67b8879 commit 78e0433

File tree

10 files changed

+433
-44
lines changed

10 files changed

+433
-44
lines changed

.gitattributes

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
bpf/bpf_helper_defs.h linguist-generated=true
2-
bpf/bpf_helpers.h linguist-generated=true
1+
bpf/bpf_core_read.h linguist-vendored
2+
bpf/bpf_helper_defs.h linguist-vendored
3+
bpf/bpf_helpers.h linguist-vendored
4+
bpf/handler-bpfeb.o linguist-generated
5+
bpf/handler-bpfel.o linguist-generated
6+
bpf/vmlinux.h linguist-vendored

LICENSE

Lines changed: 338 additions & 5 deletions
Large diffs are not rendered by default.

LICENSE.GPL

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (C) 2021 Coder Technologies, Inc.
1+
Copyright (C) 2022 Coder Technologies, Inc.
22

33
This program is free software; you can redistribute it and/or modify
44
it under the terms of the GNU General Public License as published by

LICENSE.MIT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (C) 2021 Coder Technologies, Inc.
3+
Copyright (C) 2022 Coder Technologies, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,116 @@
1-
# exectrace [![Go Reference](https://pkg.go.dev/badge/cdr.dev/exectrace.svg)](https://pkg.go.dev/cdr.dev/exectrace)
1+
# exectrace [![Go Reference](https://pkg.go.dev/badge/github.com/coder/exectrace.svg)](https://pkg.go.dev/github.com/coder/exectrace)
22

3-
Simple [eBPF](https://ebpf.io/)-based exec snooping on Linux, packaged as a Go
3+
Simple [eBPF](https://ebpf.io/)-based exec snooping on Linux packaged as a Go
44
library.
55

6-
exectrace loads a precompiled [eBPF program](./bpf/handler.c) into the running
6+
exectrace loads a pre-compiled [eBPF program](./bpf/handler.c) into the running
77
kernel to receive details about the `exec` family of syscalls.
88

9-
## Installation
9+
## Requirements
1010

11-
exectrace only support Go 1.16+ and Linux kernel 5.8+ (due to use of
11+
exectrace only supports Go 1.16+ and Linux kernel 5.8+ (due to the use of
1212
`BPF_MAP_TYPE_RINGBUF`).
1313

14-
```
15-
$ go get -u cdr.dev/exectrace
14+
## Installation
15+
16+
```console
17+
$ go get -u github.com/coder/exectrace
1618
```
1719

18-
## Quick Start
20+
## Quickstart
1921

20-
You will need root access, `CAP_SYS_ADMIN` or `CAP_BPF` to run eBPF programs on
22+
You will need root access, `CAP_SYS_ADMIN` or `CAP_BPF`, to run eBPF programs on
2123
your system.
2224

23-
> tip: you can use `go run -exec sudo ./cmd/program` to compile a program and
25+
> Use `go run -exec sudo ./cmd/program` to compile a program and
2426
> start it with `sudo`
2527
26-
```
27-
$ go install -u cdr.dev/exectrace/cmd/exectrace
28+
```console
29+
$ go install -u github.com/coder/exectrace/cmd/exectrace
2830
$ exectrace --help
2931
...
3032

3133
$ sudo exectrace
3234
2021/12/01 16:42:02 Waiting for events..
33-
[1188921, comm="node"] /bin/sh -c 'which ps'
34-
[1188922, comm="sh"] which ps
35+
[1188921, comm="node", uid=1002, gid=1003] /bin/sh -c 'which ps'
36+
[1188922, comm="sh", uid=1002, gid=1003] which ps
3537
```
3638

3739
## Usage
3840

39-
You can look at the example program [exectrace](./cmd/exectrace/main.go) for a
40-
comprehensive program using this library.
41+
exectrace exposes a minimal API surface. Call `exectrace.New(nil)` and then
42+
you can start reading events from the returned `Tracer`.
43+
44+
It is important that you close the tracer to avoid leaking kernel resources,
45+
so we recommend implementing a simple signal handler like the one in this
46+
example:
47+
48+
```go
49+
package main
50+
51+
import (
52+
"fmt"
53+
"os"
54+
"os/signal"
55+
"syscall"
56+
57+
"github.com/coder/exectrace"
58+
)
59+
60+
func main() {
61+
tracer, err := exectrace.New(nil)
62+
if err != nil {
63+
panic(err)
64+
}
65+
defer tracer.Close()
66+
67+
go func() {
68+
sigs := make(chan os.Signal, 1)
69+
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
70+
<-sigs
71+
tracer.Close()
72+
}()
73+
74+
for {
75+
event, err := tracer.Read()
76+
if err != nil {
77+
panic(err)
78+
}
79+
80+
fmt.Printf("%+v\n", event)
81+
}
82+
}
83+
```
4184

42-
## Development
85+
> For a full usage example, refer to this [comprehensive program](./cmd/exectrace/main.go)
86+
> that uses the library.
4387
44-
Since the eBPF program is packaged as a Go library, the program needs to be
45-
compiled and included in the repo. If you make changes to files under the `bpf`
46-
directory, you should run `make` and include the `.o` files in that directory in
47-
your commit if they changed. CI will ensure that this is done correctly.
88+
## Development
4889

49-
You will probably need the following tools:
90+
You will need the following:
5091

51-
- Docker (clang is run within a Docker container for reproducibility)
92+
- Docker (the Makefile runs clang within a Docker container for reproducibility)
5293
- `golangci-lint`
5394
- `prettier`
5495
- `shellcheck`
5596

56-
## Status: In Development
97+
Since the eBPF program is packaged as a Go library, you need to compile the
98+
program and include it in the repo.
99+
100+
If you change the files in the `bpf` directory, run `make` and ensure that you
101+
include the `.o` files you changed in your commit (CI will verify that you've
102+
done this correctly).
103+
104+
## Status: beta
57105

58-
The library is currently under heavy development as we develop it out to suit
59-
the needs of Coder's enterprise [product](https://coder.com).
106+
This library is ready to use as-is, though it is under active development as we
107+
modify it to suit the needs of Coder's [enterprise product](https://coder.com).
60108

61-
We plan on changing the API to add more features and fields that can be read
62-
from, and potentially adding easier methods for filtering events rather than
63-
implementing filtering yourself.
109+
We plan on adding more features and fields that can be read from the API, as
110+
well as easier-to-use methods for filtering events (currently, you must
111+
implement additional filtering yourself).
64112

65-
## See Also
113+
## See also
66114

67115
- [`canonical/etrace`](https://github.com/canonical/etrace) - Go binary that
68116
uses ptrace and tracks the processes that a command launches for debugging and
@@ -72,4 +120,4 @@ implementing filtering yourself.
72120

73121
---
74122

75-
Dual licensed under the MIT and GPL-2.0 licenses. See [LICENSE](LICENSE).
123+
Dual licensed under the MIT and GPL 2.0 licenses. See [LICENSE](LICENSE).

bpf/handler-bpfeb.o

152 Bytes
Binary file not shown.

bpf/handler-bpfel.o

152 Bytes
Binary file not shown.

bpf/handler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ s32 enter_execve(struct exec_info *ctx) {
161161
u64 uidgid = bpf_get_current_uid_gid();
162162
u64 pidtgid = bpf_get_current_pid_tgid();
163163
event->uid = uidgid; // uid is the first 32 bits
164-
event->gid = uidgid << 32; // gid is the last 32 bits NOLINT(readability-magic-numbers)
164+
event->gid = uidgid >> 32; // gid is the last 32 bits NOLINT(readability-magic-numbers)
165165
event->pid = pidtgid; // pid is the first 32 bits
166166
ret = bpf_get_current_comm(&event->comm, sizeof(event->comm));
167167
if (ret) {

cmd/exectrace/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/spf13/cobra"
1515
"golang.org/x/xerrors"
1616

17-
"cdr.dev/exectrace"
17+
"github.com/coder/exectrace"
1818
)
1919

2020
func main() {
@@ -92,7 +92,11 @@ func run(pidNS uint32, outputFormat string) error {
9292
ellipsis = "..."
9393
}
9494

95-
_, _ = fmt.Printf("[%v, comm=%q] %v%v\n", event.PID, event.Comm, shellquote.Join(event.Argv...), ellipsis)
95+
_, _ = fmt.Printf(
96+
"[%v, comm=%q, uid=%v, gid=%v] %v%v\n",
97+
event.PID, event.Comm, event.UID, event.GID,
98+
shellquote.Join(event.Argv...), ellipsis,
99+
)
96100
continue
97101
}
98102
err = enc.Encode(event)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module cdr.dev/exectrace
1+
module github.com/coder/exectrace
22

33
go 1.16
44

0 commit comments

Comments
 (0)