Commit graph

640 commits

Author SHA1 Message Date
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
81dd625a1c
added more kernel config paths for checking system requirements
On Fedora Silverblue the kernel config of the current kernel is under
/usr/lib/modules/<kernel>/config

Closes: #1117
2024-04-27 23:36:04 +02:00
Gustavo Iñiguez Goia
785500cd08
clean dns ebpf hooks on exit
We were not reacting to common exit signals, only to kill/interrupt
signals, so the DNS uprobes were never properly removed. Each uprobe
has the PID of the daemon in the identifier, so in theory, there
shouldn't be conflicts, but better clean our probes on exit.

previous to this commit with the daemon running
(and lot of starts/stops):

~ # cat /sys/kernel/debug/tracing/uprobe_events |wc -l
367

after stopping the daemon:
~ # cat /sys/kernel/debug/tracing/uprobe_events |wc -l
364

~ # > /sys/kernel/debug/tracing/uprobe_events
~ # cat /sys/kernel/debug/tracing/uprobe_events |wc -l
0

~ # cp opensnitchd-new /usr/bin/opensnitchd ; service opensnitchd start
~ # cat /sys/kernel/debug/tracing/uprobe_events |wc -l
3
~ # service opensnitchd stop
~ # cat /sys/kernel/debug/tracing/uprobe_events |wc -l
0
2024-01-28 01:10:00 +01:00
Gustavo Iñiguez Goia
55678b4d3b
added more debug info to the dns ebpf hook
Added the path to the libc as well as the calculated offset for the
uprobe.
Don't return on the first error found loading a uprobe, instead try all
the uprobes and return if the loaded uprobes are 0.
2024-01-26 14:48:44 +01:00
Gustavo Iñiguez Goia
c1ba2add20
fixed debug msg printing resolved IP 2024-01-25 16:00:49 +01: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
379d010ab8
minor improvement/refactoring
Move queues setup to its own func.
Don't declare some dns vars inside for loops.
2024-01-18 11:37:07 +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
dc43d5913c
allow to configure GC percentage
Added config option to set how often the garbage collector runs.

For example:

  "Internal": {
      "GCPercent": 75
  },

If this option is not specified in the config file, or the value
is 0, then the GC percentage is not configured.

More info:

https://pkg.go.dev/runtime/debug#SetGCPercent
2024-01-18 00:01:58 +01:00
Gustavo Iñiguez Goia
a389707eb6
added more options to debug the daemon
- Improved -mem-profile icli option for writing memory statistics.
 - Added -trace-file cli option for writing trace events.

https://github.com/google/pprof/blob/main/doc/README.md
https://go.dev/blog/pprof
2024-01-17 21:31:55 +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
e8dffbe11c
fixed typo 2024-01-15 20:45:17 +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
bb95a77c58
sys-fw: load default config path if non supplied
By default load the system fw config file from
/etc/opensnitchd/system-fw.json.

There're these options to specify the file to load:

 - via cli option with -fw-config-file
 - writing it in the default-config.json file:
   "FwOptions": { "ConfigPath": "..." }

If both options are empty, then the default one is used.

FIXME:
 When the cli option is used to load the fw configuration, and the main
preferences are saved, the fw is reloaded but the path to the fw config
is lost.
2024-01-08 00:34:28 +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
159494d1e2
fixed loading dns ebpf module
Closes #1076
2024-01-04 01:12:03 +01:00
Gustavo Iñiguez Goia
050e3c51b0
fixed leak dispatching alerts
Fixed potential leak when disconnecting from the GUI, and at the same
time dispatching an alert/message to the GUI.
2023-12-31 20:12:44 +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
090bb0e2a4
updated sys-fw tests 2023-12-20 23:02:45 +01:00
Gustavo Iñiguez Goia
54ac5a3549
fw: allow to configure config file/ check interval
- Allow to configure system firewall configuration file path:
   * via cli (-fw-config-file).
   * via global configuration file.
 - Allow to configure fw rules check interval.

The system fw config file contains regular iptables/nftables rules.
Previously it was hardcoded to /etc/opensnitchd/system-fw.json

The interval to check if the interception rules were added was also
hardcoded to 10 seconds. Now it's possible to configure it.
A value of "0s" disables the interval, while "" defaults to 10 seconds.
2023-12-20 21:32:45 +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
6873fd3a2d
updated ui/client tests 2023-12-17 00:39:41 +01:00
Gustavo Iñiguez Goia
211c864d76
allow to configure rules and config file paths
- Added cli option -config-file to specify an alternate path to the
   config file.
 - Allow to configure rules path from the configuration file (cli option
   takes precedence).
 - Default options are now /etc/opensnitchd/rules and
   /etc/opensnitchd/default-config.json. Previously the default rules
   directory was "rules" (relative path).

Closes #449
2023-12-17 00:22:07 +01:00
Gustavo Iñiguez Goia
9cee3b3a73
fixed leak checking fw status 2023-12-15 11:49:16 +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
9efaa37098 fixed leak serializing rules' operator
mainly when connecting by the first time to the GUI, and at the same
time asking to allow/deny a connection.
2023-12-09 19:06:40 +01:00
Gustavo Iñiguez Goia
17c8ec8484
fixed leak getting proc parents 2023-12-06 00:12:41 +01:00
Gustavo Iñiguez Goia
430c280687
netfilter: do not unbind the queues on exit
On exit we were calling nfq_unbind_pf for AF_INET and AF_INET6, which
usually stcuked the daemon for a very long time, and other times
caused a segfault.

According to the docs (..), calling nfq_destroy_queue() is enough to
exit cleanly:
"This call also unbind from the nfqueue handler, so you don't have to
call nfq_unbind_pf."
https://netfilter.org/projects/libnetfilter_queue/doxygen/html/group__Queue.html#ga32a1461e9a36c95a03cb4476109f33bb

Closes #919
2023-12-05 01:16:38 +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
2fc9ed276b
deserialize rules operator list correctly
In b93051026e we disabled sending/parsing
list operators as JSON strings. Instead, now it's sent/parsed as
protobuf Rule, and saved to disk as JSON array, which ease the task of
manually creating new rules if needed.

This change was missing in the previous commit.
2023-11-11 02:16:08 +01:00
Gustavo Iñiguez Goia
6b3a5da343
Control checksums feature from a global cfg item
Previously it was enabled only if at least 1 rule had a checksum item
configured.
Enable by default md5.
2023-11-10 11:28:54 +01:00
Gustavo Iñiguez Goia
6d056b8a7b
Bump daemon and GUI versions
Current changes are no longer compatible with 1.6.x.
2023-11-09 01:05:46 +01:00
Gustavo Iñiguez Goia
5b2cec0d4b
go.mod: updated deps, minimum required go version 2023-10-23 20:50:24 +02:00
Gustavo Iñiguez Goia
7d8eb36f60
Merge branch 'master' into oscs_fix_cjri7kgau51vtpiu8un0 2023-10-23 18:41:48 +00:00
Gustavo Iñiguez Goia
0a01e44870
Merge pull request #992 from chncaption/oscs_fix_cit2hp0au51ueorq4bpg
fix(sec): upgrade golang.org/x/sys to 0.1.0
2023-10-23 18:40:53 +00:00
Gustavo Iñiguez Goia
40b738ffea
updated rules tests 2023-10-09 18:07:48 +02:00
Gustavo Iñiguez Goia
b93051026e
rules: improved operator list parsing and conversion
Previously when creating a new rule we followed these steps:
 - Create a new protobuf Rule object from the ruleseditor or the
   pop-ups.
 - If the rule contained more than one operator, we converted the
   list of operators to a JSON string.
 - This JSON string was sent back to the daemon, and saved to the
   DB.
 - The list of operators were never expanded on the GUI, i.e., they
   were not saved as a list of protobuf Operator objects.
 - Once received in the daemon, the JSON string was parsed and
   converted to a protobuf Operator list of objects.
   Both, the JSON string and the list of protobuf Operator objects were
   saved to disk, but the JSON string was ignored when loading the
   rules.

Saving the list of operators as a JSON string was a problem if you
wanted to create or modify rules without the GUI.

Now when creating or modifying rules from the GUI, the list of operators
is no longer converted to JSON string. Instead the list is sent to the
daemon as a list of protobuf Operators, and saved as JSON objects.

Notes:
 - The JSON string is no longer saved to disk as part of the rules.
 - The list of operators is still saved as JSON string to the DB.
 - About not enabled rules:
   Previously, not enabled rules only had the list of operators as JSON
   string, with the field list:[] empty.
   Now the list of operators is saved as JSON objects, but if the rule
   is not enabled, it won't be parsed/loaded.

Closes #1047
2023-10-09 14:55:15 +02: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
c22e358fce
added CONFIG_PROC_EVENTS to check-requirements
Check if the kernel has support for the Process Events Connector feature,
to see if we can use it.

https://lwn.net/Articles/157150/
2023-09-30 21:59:45 +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
7a04a53eb1
display checksums on the process dialog
Display the checksums on the process details dialog.
Improved process' icon discovery.
2023-09-23 12:26:21 +02:00
Gustavo Iñiguez Goia
81a964bf83
updated rules tests 2023-09-22 01:21:27 +02:00
Gustavo Iñiguez Goia
1176fa5b9c
updated go.mod
Bumped gopacket and netlink versions.
2023-09-22 01:05:20 +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