Commit graph

177 commits

Author SHA1 Message Date
Gustavo Iñiguez Goia
8bd8f59fb5
fixed "non-constant format string" error 2026-07-06 14:56:26 +02:00
Gustavo Iñiguez Goia
df8390e57f
minor performance improvements
Replaced calls Sprint/Sprintf in hot paths with internal ConcatStrings,
improving string concatenation performance by approximately ~2x-~10x.
2026-07-06 01:46:11 +02:00
dan
9c0c2545fe tests: added netfilter tests 2026-01-01 19:49:30 +02:00
dan
2c01e9dd56 tests: added ebpf integration tests 2026-01-01 11:32:47 +02:00
Gustavo Iñiguez Goia
42600fc163 add the current process to the tree when re-using it
When re-using the tree of a process, we were not adding the current
process to the tree.
2025-12-30 17:14:20 +01:00
Gustavo Iñiguez Goia
fbc72bb96a
mount tracefs path if it's not mounted
Some kernels or systems may not have tracefs mounted or available
by default.

We need the path /sys/kernel/tracing to be mounted, in order to add
syscalls ebpf probes.

So from now on we'll try to mount /sys/kernel/tracing if it's not
already mounted.

-check-requirements parameter updated to check if this option is
available.

Closes #1450.
2025-11-23 23:40:37 +01:00
Grimmauld
ec75833dd5
daemon/audit: allow configuring audispd_events socket path 2025-08-26 20:30:02 +02:00
Gustavo Iñiguez Goia
ca2c56d5cd cache: improved replacing processes
When replacing a process with the same PID (fork/clone),
and ebpf.QueueEventsSize is > 0 we may receive out or order events.
For example if the checksum calculation takes too much time.

Now we avoid updating the process in this scenario.
2025-07-30 16:57:32 +02:00
Gustavo Iñiguez Goia
7eb1cbf90d
improved proc monitors errors
- Avoid sending duplicated errors.
 - Fixed sending error message when the ebpf events module fails to
   load.
2025-06-22 22:23:36 +02:00
Gustavo Iñiguez Goia
f1318ef12d
fixed loading config when the procmon fails
- Fixed configuration loading, when the Process Monitor Method
   selected failed to work (e.g.: when ebpf modules are missing,
   auditd is not configured, etc).
   In this scenario, on the first run, the daemon was not loading the
   configuration from disk, and was instead using the default values, which
   led to some glitches (unexpected reconnections to the GUI, checksums
   option ignored, etc).
 - added more tests.
2025-06-18 00:35:17 +02:00
Gustavo Iñiguez Goia
9f570ea9ad ebpf events improvements
Discard events from the queue if we can't send events to the channel.
It can happen if the case of a deadlock such as in ac3053af327e38acad4ff4024ed688ed68d6ae30

In this situation, reading from the ring buffer is blocked, which
eventually causes it to fill up.
When the ring buffer is full, bpf_ringbuf_output() fails with error -11.
2025-06-15 23:10:09 +02:00
Gustavo Iñiguez Goia
c198f0119f improved events cache
Under very high load of kernel events, combined with a
non-zero value for the ebpf.QueueEventsSize config option,
the cache could end up blocking the events workers if the option to
compute process checksums was enabled.
2025-06-15 23:10:09 +02:00
Gustavo Iñiguez Goia
f0b1173d9d added Process path+args tests 2025-06-12 23:43:59 +02:00
Gustavo Iñiguez Goia
376b06ef97
improved binary absolute path retrieval
When receiving the paths of a binary from kernel, the path can be a
symlink to the real binary on disk, and we were not obtaining the real
path to the binary.

Now we'll try to obtain it. If we fail, we'll use the path received from
the kernel. And if it's empty, we'll fallback to read it from proc.

Closes #1357
2025-06-07 23:51:13 +02:00
Gustavo Iñiguez Goia
ef926050c2
fixed listening for link changes (#1343)
netlink.AddrSubscribeWithOptions() was causing systemd-resolved not to
obtain DNS servers, and therefore the system was unable to resolve
domain names.

This ssue started happening on Fedora 42, with systemd-resolved 257 and
kernels 6.14.x.

Using netlink.AddrSubscribe() solves this issue for now.

Closes #1343
2025-05-23 12:53:29 +02:00
Gustavo Iñiguez Goia
b20fd15335
changed ebpf log levels
Changed Error to Warning log level when failing to load hooks.

Except for udp_tunnel6_xmit_skb, which depends on the module
ip6_udp_tunnel to be loaded, which by default is not the case.
In order not to flood the logs with this error, log level changed to
Debug.
2025-05-21 21:49:15 +02:00
Gustavo Iñiguez Goia
56b2d28b66
ebpf: added support for ipv6 tunnels
In order to intercept UDP tunnels traffic, we use iptunnel_xmit for IPv4
connections. The equivalent of IPv6 would be ip6tunnel_xmit, but
unfortunately it's not public.

So instead of ip6tunnel_xmit, we'll use udp_tunnel6_xmit_skb.

This function receives two parameters besides the struct sock*, in6_addr
*saddr and in6_addr *daddr. If these addresses are not set, usually the
connection is a localhost connection.

On the other hand, this function is defined in the module
ip6_udp_tunnel. If this module is not loaded when the daemon starts,
we'll fail to intercept the connections.

Closes: #1250
2025-05-05 00:34:25 +02:00
Gustavo Iñiguez Goia
568d69520f
fixed reporting connection UID
When getting a connection from the cache, use the UID of the connection,
instead of the process UID.

more info: https://github.com/evilsocket/opensnitch/issues/1315#issuecomment-2829072245

Closes: #1315
2025-04-27 23:54:52 +02:00
Gustavo Iñiguez Goia
aeb2018d27
log the UID of exec events 2025-04-24 23:55:42 +02:00
Gustavo Iñiguez Goia
12c21c7a26
removed debug line 2025-04-24 23:53:50 +02:00
Gustavo Iñiguez Goia
82b09c3ac6
removed not needed log line 2025-04-24 18:00:31 +02:00
Gustavo Iñiguez Goia
2a8f13d439
process path parsing improvements
- Preprend the chroot of a process to the path:
   Even if a process is executed from a chroot (or a different mount
   namespace), the kernel reports the path as it were from the host.
   For chrooted processes, now we prepend the chroot to the path:
   Instead of /usr/bin/curl -> /var/cache/pbuilder/usr/bin/curl

   Note: if the process is emulated (with qemu for example), the
   checksum will be of the emulator.

 - For proceses launched from /proc, like /proc/self/exe or
   /proc/<pid>/fd/<number>, we were replacing in the cmdline "/proc/*"
   with the absolute path of the binary:

   Path: /usr/bin/curl
   Cmdline reported: /proc/<pid>/fd/<number> -L 1.1.1.1
   Cmdline "fixed": /usr/bin/curl -L 1.1.1.1

   Now we only do that for /proc/self/exe, and leave
   /proc/<pid>/fd/<number> as is, since Path is already the absolute
   path to the binary.

   Path: /usr/bin/spotify
   Cmdline reported: /proc/self/exe
   Cmdline "fixed": /usr/bin/spotify

   Path: /usr/bin/curl
   Cmdline reported: /proc/<pid>/fd/<number> -L 1.1.1.1
   Cmdline final: /proc/<pid>/fd/<number> -L 1.1.1.1
2025-04-18 13:30:51 +02:00
Gustavo Iñiguez Goia
8ec86c638c
ebpf: arm improvements
- better process interception on aarch64 and armhf.
 - on armhf getting proc arguments is ignored due to an error, but we
   were not marking the event as INCOMPLETE_ARGS, so we were not reading
   the compete cmdline.
2025-04-14 17:43:41 +02:00
Gustavo Iñiguez Goia
452233901d
avoid printing in debug possible invalid var 2025-04-10 21:26:26 +02:00
Gustavo Iñiguez Goia
feafe87604
migrated ebpf lib from gobpf to cilium
Up until now we've been using https://github.com/iovisor/gobpf/ for
intercepting events from the kernel, and it has worked very well.

Unfortunately it's deprecated since 2021 (iovisor/gobpf#304), which means
that some of the known issues and limitations won't be fixed.

After evaluating some options, I've migrated the code from gobpf to
github.com/cilium/ebpf.

More info: #1222, #1312
2025-04-09 00:15:38 +02:00
Gustavo Iñiguez Goia
f23c1635ca
minor logging improvements
- Moved Info message to Trace.
 - When applying a default action on a connection (because the daemon is
   not connected to the GUI), log the connection details. It eases the
   task of creating rules without a GUI.
2025-03-04 22:19:07 +01:00
Gustavo Iñiguez Goia
8d1a7de0cc
moved some Debug msgs to Trace 2025-03-03 21:35:31 +01:00
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