Commit graph

49 commits

Author SHA1 Message Date
Alan Parra 9f15d572eb
Apply various small BPF refactors (#31977)
* Replace magical `4` and `6` numbers

* Add host-to-net.IP conversion helpers

* Add a Vagrantfile for BPF testing

* Move reexec logic out of lib/bpf/common_test.go

* Refactor TestBPFConfig_CheckAndSetDefaults

* Move Vagrantfile to bpf/
2023-09-15 21:33:03 +00:00
rosstimothy f6e9ca269e
Log the value of EventsBufferSize instead of the pointer address (#29031) 2023-07-12 22:11:37 +00:00
Andrew LeFevre d4b3afe9a1
split recording session events and emitting audit events (#27873)
* split recording session events and emitting audit events

This is a refactor of how audit events and session events are handled.
Previously, all events were emitted using the same interface,
api/types/events.Emitter. This lead to event-related code getting to be
very confusing, as it was often unclear whether a given event was being
recorded as a session event and emitted as an audit event, or only one
of the two. Naturally, a few bugs arose due to this.

To simplify event handling, a separate interface for recording session
events has been created. A api/types/events.Recorder should now only be
used to record session events, and an Emitter should now only be used to
emit audit events. Instead of using a confusing TeeWriter that would
transparently (and confusingly, given its name) hold a few event types
that only belonged in session recordings, callers can now explicitly
record and/or emit an event when necessary.

* ensure e build won't break
2023-07-11 19:53:33 +00:00
Edoardo Spadolini d2288d0750
Don't unmount cgroup2 when restarting (#26610)
* Don't unmount cgroup2 when restarting

* Add a test for skipUnmount
2023-05-22 19:42:12 +00:00
Jakub Nyckowski a31245a70e
Temporary disable BPF tests (#23428)
BPF tests seem to be flaky after some recent changes. I'll look into the issue, but I want to temporarily disable all tests to not block other people from work.
2023-03-22 13:12:36 +00:00
Jakub Nyckowski 35a5688a79
Move BPF event filtering to the kernel (#23017)
* Move BPF event filtering to the kernel

#19354 moved filtering of disk events to the kernel space. This PR continues work in this area and moves all events to be filtered in the kernel space.

* Improve comments.
Minor code fixes.
2023-03-20 18:46:00 +00:00
Zac Bergquist 2ae9d770fc
Refactor tctl's dependencies (#22693)
* Move configuration from lib/service to lib/service/servicecfg

The new servicecfg package will hold only configuration for services.
This will allow other packages (like tctl and tsh) to depend on
servicecfg without pulling in all of lib/service (which has a number
of platform-specific details).

This is the first step towards being able to build tctl for Windows.

* Move PAM and BPF config into servicecfg

This breaks a compile-time dependency on BPF/PAM for tctl.
2023-03-09 17:48:36 +00:00
Jakub Nyckowski 1b0b30e447
Create separate build tags for different arch (#21086)
Test ARM64 assets build

Use all available cores when building Clang

Add test trigger

Update assets buildbox name

Build all dependencies on ARM64 including BPF
2023-02-23 17:11:50 +00:00
Jakub Nyckowski 7fab8fadc6
Currently, our BPF modules send all events from the kernel to the userspace, where we filter them and log only the ones related to our process. Sending the events between the user and kernel space is expensive, and most of the events are discarded after. (#19354)
This PR moves the filtering from the userspace to the kernel, where we can filter them earlier and not pay for sending all events to our userspace process. Because the filtering happens in the kernel, the BPF test had to be rewritten to execute events in a sub-cgroup instead of the global one.
2023-02-07 19:20:55 +00:00
Jakub Nyckowski 8ed297f0a3
Restore BPF network events (#20150)
Networks BPF events were disabled in #18497, which also removed them from our audit log when the restricted session is disabled. This PR reverts the logic and fixes that behavior.
2023-01-13 16:28:40 +00:00
Vitor Enes c96809399a
Add server_hostname to session.* events (#18812)
With this commit, `session.*` events now also contain a `server_hostname` field.

Example:

```json
{
  "argv": null,
  "cgroup_id": 6926,
  "cluster_name": "vitor.teleport.sh",
  "code": "T4000I",
  "ei": 17,
  "event": "session.command",
  "login": "root",
  "namespace": "default",
  "path": "/usr/bin/date",
  "pid": 98679,
  "ppid": 98668,
  "program": "date",
  "return_code": 0,
  "server_hostname": "ip-172-31-10-157",
  "server_id": "8369d1d1-eea5-4074-8f64-0103f1e1fb00",
  "sid": "a40353c4-93dc-4d6b-a481-5546ecafb83e",
  "time": "2022-11-28T16:32:34.342Z",
  "uid": "cbd9e562-8932-405d-974c-11637f91d711",
  "user": "vitor@goteleport.com"
}
```
2022-11-28 20:28:41 +00:00
Jakub Nyckowski 506e0db534
Enable network BPF modules only when needed. (#18497) 2022-11-25 18:33:40 +00:00
Jakub Nyckowski bea2e89df5
Run GCI as make fix-imports (#17956)
Co-authored-by: Alan Parra <alan.parra@goteleport.com>
2022-11-04 15:46:46 +00:00
Jakub Nyckowski 0ee91f6c37
Enable GCI linter (#17894) 2022-10-28 20:20:28 +00:00
Jakub Nyckowski 716c2bdbf9
Fix BPF (#17269)
* Revert "Refactor bpf/restrictedsession usage in lib/srv (#15824)"

This reverts commit 769951fe4b.

* Remove unused Linux BPF includes

* Fix linter issue
2022-10-12 18:41:26 +00:00
Zac Bergquist bb0000441f
Improve BPF tests (#16939)
This doesn't address the flakiness in TestRootScript, but it does:

- ensure that the background goroutine terminates
- ensure that test failures are only done in the main goroutine
- fix a race condition due to the shadowing of the 'err' variable

Updates #16908
2022-10-03 17:53:53 -06:00
rosstimothy 769951fe4b
Refactor bpf/restrictedsession usage in lib/srv (#15824)
Invert the relationship of `lib/srv` and `lib/bpf`,
`lib/restrictedsession` such that `lib/bpf` is only imported in
`lib/srv/regular`. Since not everything is built with the `bpf`
tag it's important to reduce the surface area of `lib/bpf` such
that it isn't inadvertantly imported. For instance it was entirely
possible to import a package in `tsh` that transitively
depends on `lib/bpf` - which breaks the build since `tsh` is not
compiled with the `bpf` tag.

By refactoring the `srv.Server` interface not to use the `bpf.BPF`
and `restrictedsession.Manager` interfaces directly anything that
imports `lib/srv` now won't require that `-tags=bpf` is set in
order to compile.
2022-09-07 14:49:28 +00:00
Russell Jones 7842f8b569 Fix. 2022-08-18 15:22:10 -07:00
Russell Jones c3c2ca2c96 Fix. 2022-08-18 15:22:10 -07:00
Russell Jones 4fa6d5acdd Refactor tests under bpf tests
Refactored all tests under "lib/bpf" and "lib/restrictedsession" to use
testify instead of gocheck.
2022-08-18 15:22:10 -07:00
rosstimothy c469a34994
Move prometheus collectors from utils to metrics (#15288) 2022-08-09 17:35:19 +00:00
Jakub Nyckowski 19d6207919
Docs fixes in BPF module (#15287) 2022-08-08 17:28:18 +00:00
Jakub Nyckowski c380f86bd9
Enable BPF tests in CI (#10933) 2022-07-15 00:16:56 +00:00
Eugene Yakubovich 4de14faa13
Bump to libbpfgo v0.2.5 (#12288)
Co-authored-by: Jakub Nyckowski <jakub.nyckowski@goteleport.com>
2022-06-08 05:00:01 +00:00
Zac Bergquist 7e309e9ae2
Remove unused constants (#12033) 2022-04-19 18:59:35 +00:00
Zac Bergquist 3b0127f00b Move MockEmitter to eventstestpackage 2022-03-29 12:12:01 -06:00
Zac Bergquist 3f507dfd06 Remove uses of deprecated ioutil package 2022-03-16 15:05:42 -06:00
Jakub Nyckowski 6930ee9b98
Disable BPF tests in CI (#10654)
Run BPF tests only if TELEPORT_BPF_TEST environment variable is set. This should prevent on running those tests on machines that doesn't support BPF.
2022-02-28 18:14:38 +00:00
Joel 62173e096b
use google/uuid instead of pborman/uuid (#9793)
* replace imports

* use google/uuid

* fix test

* reverse changelog changes

* update gomod

* zac steps

* tidy

Co-authored-by: Zac Bergquist <zac.bergquist@goteleport.com>
2022-01-19 23:44:48 +00:00
Zac Bergquist 0bae0d255f Cleanup lint targets
- Don't assume an explicit $GOPATH is set
- Remove golint from linters - it's been deprecated for over a year
  and golangci-lint prints a warning instead of running it.
2021-10-19 09:21:55 -06:00
Brian Joerger 9b8b9d6d0c
rollback - Upgrade api version. (#7751) 2021-07-30 15:34:19 -07:00
Russell Jones 628e96aa61 Updated BPF asset embedding.
Updated how BPF assets are embedded. Missing object files will now be a
runtime error instead of a build time error. However, this will allow
teleport/lib to used as a module because tools like "go mod vendor" were
failing when attempting to vendor teleport/lib due to the missing object
files.

Added bytecode directory with README.md so "go:embed" does not complain
about missing assets. This allow developers to checkout Teleport and
work on it without needing libbpf or any other BPF tooling.
2021-07-28 17:16:14 -07:00
Brian Joerger c040aca4c1
Upgrade api version. (#7609) 2021-07-28 13:51:21 -07:00
Eugene Yakubovich 67c0eb3b4c Add restricted session
Adds the ability to block network traffic on SSH sessions.
The deny/allow lists of IPs are specified in teleport.yaml file.
Supports both IPv4 and IPv6 communication.

This feature currently relies on enhanced recording for
cgroup management so that needs to be enabled as well.

-- Design rationale:
This patch uses Linux Security Module (LSM) hooks, specifically
security_socket_connect and security_socket_sendmsg, to control
egress traffic. The LSM provides two advantages over socket filtering
program types.
- It's executed early enough that the task information is available.
  This makes it easy to report PID, COMM, etc.
- It becomes a model for extending restrictions beyond networking.

The set of enforced cgroups is stored in a BPF hash map and the
deny/allow lists are stored in BPF trie maps. An IP address is
first checked against the allow list. If found, it's checked for
an override in the deny list. The policy is default deny. However,
the absence of the NetworkRestrictions API object is allow all.

IPv4 addresses are additionally registered in IPv6 trie (as mapped)
to account for dual stacks. However it is unclear if this is sufficient
as 4-to-6 transition methods utilize a multitude of translation and
tunneling methods.
2021-07-16 16:49:04 -07:00
Eugene Yakubovich c83d028d92 libbpfgo has been moved out of tracee
libbpfgo is now a standalone project and thus
requires Teleport to vendor less code.
2021-07-14 11:56:59 -07:00
Brian Joerger 4d36870ff0
Remove remaining API aliases (#7137) 2021-06-08 12:08:55 -07:00
Brian Joerger 7bff7c41bd
Remove API aliases (#6983) 2021-06-04 13:29:31 -07:00
Eugene Yakubovich 585c33232b Move from BCC to libbpf with CO-RE. 2021-05-28 15:25:23 -07:00
NajiObeid 86a6abcfcb
lazy init of prometheus collectors (#6561)
* lazy init of prometheus collectors

* incorporate metrics intorduced in #6271

* linting

* tests

* pr changes

* tests

* pr changes
2021-05-19 11:53:36 -04:00
Andrew Lytvynov fc1c1dbd14 Move all utils.InitLoggerForTests calls to TestMain
This prevents data races between changing the standard logger and it
acutally being used.
2021-02-23 18:04:55 -08:00
Andrew Lytvynov 5ca68f2351
Remove 'var _ = fmt.Printf' from *_test.go files (#5438)
These declarations serve no purpose, likely leftover from old debugging.
2021-01-29 17:01:10 -08:00
a-palchikov 7c87576a8b
flaky tests: consistent logging (#4849)
* Update logrus package to fix data races
* Introduce a logger that uses the test context to log the messages so they are output if a test fails for improved trouble-shooting.
* Revert introduction of test logger - simply leave logger configuration at debug level outputting to stderr during tests.
* Run integration test for e as well
* Use make with a cap and append to only copy the relevant roles.
* Address review comments
* Update integration test suite to use test-local logger that would only output logs iff a specific test has failed - no logs from other test cases will be output.
* Revert changes to InitLoggerForTests API
* Create a new logger instance when applying defaults or merging with file service configuration
* Introduce a local logger interface to be able to test file configuration merge.
* Fix kube integration tests w.r.t log
* Move goroutine profile dump into a separate func to handle parameters consistently for all invocations
2020-12-07 15:35:15 +01:00
Sasha Klizhentas d160507430 Session streaming
This commit introduces GRPC API for streaming sessions.

It adds structured events and sync streaming
that avoids storing events on disk.

You can find design in rfd/0002-streaming.md RFD.
2020-09-28 23:08:56 -07:00
Forrest Marshall 924fb9cd00 synchronize bpf watch map reads 2020-04-01 11:41:44 -07:00
Russell Jones 995d9ffe25 Add support to disable BPF programs.
If the page size for an enhanced event is 0, then don't attempt to load
that BPF program. This is helpful for BPF programs that generate massive
amounts of events (like disk events).
2020-01-20 14:04:36 -08:00
Russell Jones 35127f797a Fix PID and parent PID swap.
PID and parent PID were swapped in command events.
2020-01-20 14:04:36 -08:00
Russell Jones a5af2d72ff More enhanced session recording error imporvements
Move BPF checks to node startup and group them togeather.
2019-12-06 18:14:26 -08:00
Russell Jones 8aaed66c26 Improve enhanced session recording error messages.
If the user enabled enhanced session recording in file configuration but
the binary was built without BPF support (like macOS) then exit right
away with a message explaining that their operating system does not
support enhanced session recording.
2019-12-06 18:14:26 -08:00
Russell Jones 77e8b63470 Enhanced Session Recording.
Added package cgroup to orchestrate cgroups. Only support for cgroup2
was added to utilize because cgroup2 cgroups have unique IDs that can be
used correlated with BPF events.

Added bpf package that contains three BPF programs: execsnoop,
opensnoop, and tcpconnect. The bpf package starts and stops these
programs as well  correlating their output with Teleport sessions
and emitting them to the audit log.

Added support for Teleport to re-exec itself before launching a shell.
This allows Teleport to start a child process, capture it's PID, place
the PID in a cgroup, and then continue to process. Once the process is
continued it can be tracked by it's cgroup ID.

Reduced the total number of connections to a host so Teleport does not
quickly exhaust all file descriptors. Exhausting all file descriptors
happens very quickly when disk events are emitted to the audit log which
are emitted at a very high rate.

Added tarballs for exec sessions. Updated session.start and session.end
events with additional metadata. Updated the format of session tarballs
to include enhanced events.

Added file configuration for enhanced session recording. Added code to
startup enhanced session recording and pass package to SSH nodes.
2019-12-02 15:10:39 -08:00