Commit graph

150 commits

Author SHA1 Message Date
Gustavo Iñiguez Goia
c6b42890c0
proc.readEnv() improvements
- Minimize the risk of race conditions when we're prompting the user to
   allow/deny a connection, while we're still reading proc's environ file.
   (this was actually a leak).
 - Preallocate the Env map with the expected environ vars.
2025-02-17 14:15:28 +01:00
Gustavo Iñiguez Goia
5eebaf40fc improved the process tree retrieval 2025-02-11 01:21:06 +01:00
Gustavo Iñiguez Goia
e403b080bc
cache,events: added debug logs
- Added logs to debug the cache of events.
 - Reduce the expected number of checksums types we may compute
   simultaneously. Probably we only need one.
2025-01-28 23:44:54 +01:00
Gustavo Iñiguez Goia
c1fdfb1d73 cache: delay the deletion time of an process
Sometimes we may receive a connection event after the exit of a
process:

[exec] /bin/xxx, pid 1234
[exit] /bin/xxx, pid 1234
[new conn] pid 1234 -> process unknown (on exec event and no /proc entry)

In these scenarios, we delay the deletion from cache a little
bit, to keep the PID available for a longer time.
2025-01-27 01:08:19 +01:00
Gustavo Iñiguez Goia
f7803ebdcb
added trace logs for packets and ebpf
- Log packets.
 - Log special case.
 - Updated information on some rare cases when intercepting connections
   via eBPF.
2025-01-22 01:06:10 +01:00
Gustavo Iñiguez Goia
b1e279fb6b
get process tree when using process conector
When the Process Connector is used to intercept exec events, get and
build the process tree of a process.

PROCESS CONNECTOR feature was added here: 7a9bb17829
2024-12-13 20:14:55 +01:00
Gustavo Iñiguez Goia
4091962770
cache of events minor fixes
- fixed storing the old proc after rebuilding the pids tree, when an
   item is replaced in cache.
 - removed not needed rlock.
2024-10-19 23:59:44 +02:00
Gustavo Iñiguez Goia
ebac200642
ReadEnv() minor improvement
- Improved peformance parsing the environment variables of a process.
 - Added ReadEnv() test.

For a 4kb environ file:

Benchmark-Old    58070 ns/op   19264 B/op   68 allocs/op
Benchmark-New    49174 ns/op   17488 B/op   12 allocs/op
2024-10-18 01:08:43 +02:00
Gustavo Iñiguez Goia
f63a48deff
calculate the ram usage of a process in the daemon
- Calculate the ram usage of a process in the daemon, using the page
   size of the system.
 - Added new functions to read some details of a process, so we can use
   them in other parts of the code.
2024-10-15 00:49:58 +02:00
Gustavo Iñiguez Goia
609edefbb7
added cache of events tests 2024-10-15 00:15:05 +02:00
Gustavo Iñiguez Goia
3bf429b9ca
getProcPids() minor improvement
reduce allocs a little bit.
2024-10-09 23:54:40 +02:00
Gustavo Iñiguez Goia
83fad69316
tasks: added sockets monitor task (netstat)
Added new task to monitor local sockets of nodes, similar to ss or
netstat.

More info: #1112
2024-10-07 23:40:40 +02:00
Gustavo Iñiguez Goia
eede54c525
allow to customize ebpf options
Allow to customize:

 - EventsWorkers: number of goroutines to handle kernel events.
   Default 8.

 - QueueEventsSize: max number of events in the queue.
   By default 0, meaning that it'll relay on the available goroutines to
   process the events. If it's > 0, and the daemon can't process the
   events fast enough, they'll be queued. Once the queue is full, it'll
   behave as it was of size 0.

If there're lost events, a message will be logged: "Lost ebpf events..."
2024-05-16 00:31:54 +02:00
Gustavo Iñiguez Goia
7d08b2b4a0
changed ui/client/configuration tests
In order to test ebpf<->proc changes we'll need to have access to a
valid ebpf module.
2024-05-06 00:10:00 +02:00
Gustavo Iñiguez Goia
bde5d34deb
reload more config options without restarting the daemon
Reload the configuration without restarting the daemon when changing:
 - server authentication options.
 - GC percentage.
 - Rules path.
 - Loggers.
 - FW options.
 - eBPF modules path.

Also, try to avoid unnecessary changes.
2024-05-02 21:14:59 +02:00
Gustavo Iñiguez Goia
be87bc538e
build parent process hierarchy of already running processes
We build the parent process tree of a process when it's executed
for the first time.
Now we also build the tree when an already running process opens a new
outbound connection by the first time.
2024-04-30 23:26:47 +02:00
Gustavo Iñiguez Goia
0a911ef791
disable (process) ebpf events when to many errors
if an invalid opensnitch-procs.o module was loaded, we were flooding
the log with errors.
In these cases stop processing events after 20 errors (random, we should
have no errors).

This may occur if the module is malformed (valid .o ebpf module but
different structs, etc), or when loading modules from other versions.

Closes: #1099 #1082
2024-04-30 00:51:41 +02:00
Gustavo Iñiguez Goia
2f1a9b8c9e
on errors stop established conns monitor
There's a long running task that monitors established connections every
~2s.

When a connection is not found via ebpf or proc, sometimes it's found
there so we can use the inode to search for the process.

However on some systems the netlink call to dump the sockets may fail
continuously, wasting resources. It'll also fail if you block connections
to port 0 (common case for ICMP packets).

So if there're too many errors dumpng the sockets, stop this task for
these cases.
2024-01-20 23:37:51 +01:00
Gustavo Iñiguez Goia
01edd361fe
process tree improvements
- When discovering the hierarchy of a process, reuse components of
   the tree if they're already on cache, to improve speed and reduce
   mem allocs.
 - When building the tree of a proces, rebuild the tree if the first
   component doesn't have pid 1. Otherwise reuse the tree.
2024-01-18 13:35:29 +01:00
Gustavo Iñiguez Goia
164696ff22
removed not used functions
part of the previous commit.
2024-01-18 01:37:08 +01:00
Gustavo Iñiguez Goia
33437672b2
ebpf cached improvements
Simplify the cache of connections by storing only the PID of a process,
instead of the Process object.

We can obtain the Process object from the cache of processes by PID.
2024-01-18 01:25:19 +01:00
Gustavo Iñiguez Goia
96e62bf973
strings concatenation improvements
Use strings.Join() to concatenate strings, instead of Sprint*(), for
better performance.
2024-01-16 00:14:44 +01:00
Gustavo Iñiguez Goia
e5787aef65
structs fields reorganized
Structs' fields reorganized based on fieldalignment tool output
2024-01-14 20:44:49 +01:00
Gustavo Iñiguez Goia
b2bd56d7e2
updated TestProcIOStats test
On this test we assumed that there would always be reading stats for our
own process /proc/self, but on restricted environments that might not
alwys be the case. Anyway, a value of 0 is not an error in itself.

Closes #1075
2024-01-05 15:31:00 +01:00
Gustavo Iñiguez Goia
68c2c8ae1a
ebpf: fixed getting ppid, skip failed execve's
- Fixed getting ppid (precompiled .o won't probably work).
 - Skip failed execve* calls.
2023-12-26 14:04:19 +01:00
Gustavo Iñiguez Goia
ffb76683aa
allow to configure ebpf modules path
Now it's possible to configure eBPF modules path from the
default-config.json file:
 "Ebpf": {
   "ModulesPath": "..."
 }

If the option is not provided, or if it's empty, we'll keep loading from
the default directories:

 - /usr/local/lib/opensnitchd/ebpf
 - /usr/lib/opensnitchd/ebpf
 - /etc/opensnitchd/ebpf (deprecated, will be removed in the future).

Closes #928
2023-12-22 23:27:18 +01:00
Gustavo Iñiguez Goia
290ed40e8d
procmon: fixed leak reading proc's environ vars 2023-12-17 23:29:27 +01:00
Gustavo Iñiguez Goia
0207e3a55f
cache of events improvement
don't reset/recalculate checksums on every configuration change, only if
the checksums config item has changed.
2023-12-12 18:11:46 +01:00
Gustavo Iñiguez Goia
431e2d3ed9
procmon/cache improvements
- Fixed several leaks.
 - Cache of events reorganized and improved.
   * items are added faster.
   * proc details are rebuilt if needed (checksums, proc tree, etc)
   * proc's tree is reused if we've got the parent in cache.

rel: #413
2023-12-12 14:48:17 +01:00
Gustavo Iñiguez Goia
17c8ec8484
fixed leak getting proc parents 2023-12-06 00:12:41 +01:00
Gustavo Iñiguez Goia
fb2c9893d0
replace /proc/self from bin paths
Sometimes we receive /proc/self/exe as the path of the process (electron
apps).
Since a couple of systemd versions ago, some processes spawned by
systemd are reported as /proc/self/fd/<number>.

In these cases reading the symbolic link /proc/<pid>/exe points to the
file on disk.
2023-12-03 01:13:55 +01:00
Gustavo Iñiguez Goia
19d376abf0
getparents code reorganization
Deoptimizing GetParents() until we figure out how to do it without
leaking mem.
2023-10-04 00:58:17 +02:00
Gustavo Iñiguez Goia
7f493e84a7
improved getting process's parents 2023-10-02 14:25:10 +02:00
Gustavo Iñiguez Goia
c8a17d6e8a
fixed monitor methods initialization
When using proc as monitor method, the ProcsEventsMonitor was not
being initialized.
2023-09-30 20:49:42 +02:00
Gustavo Iñiguez Goia
0556dc1c81
obtain process's parent hierarchy, checksums improvements
- Obtain the process's parent hierarchy.
 - Display the hierarchy on the pop-ups and the process dialog.
 - [pop-ups] Added a Detailed view with all the metadata of the
   process.
 - [cache-events] Improved the cache of processes.
 - [ruleseditor] Fixed enabling md5 checksum widget.

Related: #413, #406
2023-09-30 18:31:19 +02:00
Gustavo Iñiguez Goia
7a9bb17829
allow to filter connections by process checksum
Now you can create rules to filter processes by checksum. Only md5 is
available at the moment.

There's a global configuration option that you can use to enable or
disable this feature, from the config file or from the Preferences
dialog.

As part of this feature there have been more changes:

   - New proc monitor method (PROCESS CONNECTOR) that listens for
     exec/exit events from the kernel.
     This feature depends on CONFIG_PROC_EVENTS kernel option.

   - Only one cache of active processes for ebpf and proc monitor
     methods.

More info and details: #413.
2023-09-22 00:36:26 +02:00
Gustavo Iñiguez Goia
a828ccdcba
reorganized sockets code
Moved sockets code block to netlink package.
2023-07-24 11:18:09 +02:00
Gustavo Iñiguez Goia
f652174f75
changed addrs polling by async events
For the eBPF monitoring method, we listed and stored local addresses
every second, so that we could later check if the source IP of an
outbound connection was local or not, because sometimes we received
outbound connections like:
 443:1.1.1.1 -> 192.168.1.123:12345

This could have been alread solved on this change e090833, so maybe
we no longer need this code.

 - Now we subscribe to local addresses events, to receive add/remove
   events asynchronously, without having to list local addrs
   every second, alliviating CPU usage.

 - Fixed creating context object to cancel subroutines. It was not
   working properly when switching between proc monitor methods.
2023-07-23 21:14:47 +02:00
Gustavo Iñiguez Goia
463378c214
misc: changed the level of some messages
Also added [DNS] to some messages.

Related: #954
2023-05-28 16:23:39 +02:00
Gustavo Iñiguez Goia
474ad7636d
close ebpf perf module on stopping the monitor 2023-05-10 00:02:53 +02:00
Gustavo Iñiguez Goia
5237a2df60
lower error message to debug 2023-02-22 23:07:11 +01:00
Gustavo Iñiguez Goia
e44e1468b8
improved connections parsing, minor refactoring
When using proc monitor method + interceptUnknown, allow to ask the user
about connections not associated with a process. Usually they're safe to
discard, but on some special cases it helps not disrupt some services.

Block of code to find connections via netstat moved to procmon/
2023-02-04 16:43:24 +01:00
Gustavo Iñiguez Goia
18dc32c51f
removed ftrace monitor method
deprecated, was not in use.
2023-01-21 21:03:53 +01:00
Gustavo Iñiguez Goia
274a3483d7
improvements to ui, ebpf, netfilter
- Fixed firewall dialog label alignment.
 - Fixed potential race condition when stopping the daemon, and there're
   connections being enqueued.
 - Added "clear" button to GUI's filter line (#786)
 - Create ebpf cache object only if the modules have been loaded.
 - Set default stats workers to the sme amount defined in configuration.

Closes #785
2022-12-23 14:39:49 +01:00
Gustavo Iñiguez Goia
39473439b2
ebpf monitor: fixed crash if modules are malformed
If a eBPF module is malformed, we crashed trying reading the eBPF maps.
2022-12-22 11:40:34 +01:00
Gustavo Iñiguez Goia
7b99383546
find connections with connection fields swapped
Under certain situations, like when using systemd-resolved as DNS
resolver, we receive outbound connections with the fields swapped:

Instead of: local-port:local-ip -> public-ip:public-port
we receive: public-port:public-ip -> local-ip:local-port

Sometimes this behaviour causes network slowdowns, or no network at all.

If we swap the fields of these connections, then we're able to get the
process and keep functioning as usual. But what causes this behaviour is
yet unknown, and needs further analysis.

See these issues for more information: #779 , #711
2022-12-20 17:16:20 +01:00
Gustavo Iñiguez Goia
a45ba914e3
better ebpf tasks cancellation
It should improve daemon stopping times.
2022-12-19 18:38:13 +01:00
Gustavo Iñiguez Goia
50217afc9f
added initial support for ICMP and SCTP
Closes: 714
2022-12-18 00:41:06 +01:00
Gustavo Iñiguez Goia
2e2f1768f2
improved errors printing 2022-12-11 11:41:47 +01:00
Gustavo Iñiguez Goia
e2a822b731
better errors printing
Explain a littler bit better some errors.
2022-12-10 21:49:48 +01:00