Commit graph

647 commits

Author SHA1 Message Date
Gustavo Iñiguez Goia
f5bb478c99 use GUI's default action when applying a verdict
When the daemon connects to the GUI, use the default action
configured on the GUI to apply a verdict on new connections.

We were using daemon's default action, so if it was Allow but the user
had configured Deny on the GUI it was allowing connections, causing some
confusion (#489)

TODO: apply this action not only on connecting to the GUI, but also when
saving the configuration from the GUI.
2021-09-03 17:37:26 +02:00
Gustavo Iñiguez Goia
fa7df6595d fixed exception when cleaning up the cache
Under certain situations, while cleaning up the cache of inodes,
if an item was deleted we could end up accessing a nil item.

Spotted on ubuntu18.
2021-09-02 20:51:18 +02:00
Gustavo Iñiguez Goia
041cf456dc Bump versions to v1.4.0 2021-08-27 13:48:20 +02:00
Gustavo Iñiguez Goia
d11a98e418 fixed interception and system fw rules, default action
- The firewall rules defined in /etc/opensnitchd/system-fw.json take
precedence over the interception rules, but we were inserting the
interception rules before the system's ones. With this change #455
should be fixed.

- On the other hand, the DefaultAction was not being applied correctly
  in all cases. As of today the DefaultAction is applied in 2 scenarios:
  * When the daemon is connected to the GUI and the user doesn't answer
    a pop-up.
  * When the daemon is not connected to the GUI.

  However as we don't parse all network protocols, even if the GUI is
  connected we may end up parsing a connection and don't know what to do
  with it. In this case the DefaultAction was always Deny.

  Now in the above scenario, i.e.: when the GUI is connected but we
  can't parse a connection, we'll apply the DefaultAction configured by
  the user.
2021-08-25 13:06:32 +02:00
Gustavo Iñiguez Goia
96722ed740 blocklists: sanitize hosts entries
Remove \r\n\t\s from the end of each line of a blocklist.

If the entries of a list had these characters caused to not match
connections and not apply the rule.

closes #429
2021-08-22 23:07:25 +02:00
Gustavo Iñiguez Goia
49c171df4c Use eBPF as procs monitor method by default
If eBPF is not available we'll fallback to proc parsing.
2021-08-17 13:54:46 +02:00
Gustavo Iñiguez Goia
a354ae22e1 statistics: fixed missed connections
Previous behaviour:

 1) Before version 1.0.0b the daemon kept a list of processes that had
 established connections. The list was displayed on the GUI as is, so
 the maximum number of connections displayed were 100 (hardcoded).

 2) When the intercepted connections reached 100, the last entry of the
    list was removed, and a new one was inserted on the top.

After v1.0.0 we started saving connections to a DB on the GUI side, to
get rid of the hardcoded connections limit. However, the point 2) was
still present that caused some problems:
 - When the backlog was full we kept inserting and deleting connections
   from it continuously, one by one.
 - If there was a connections burst we could end up missing some
   connections.

New behaviour:
 - The statisics are deleted from the daemon everytime we send them to
   the GUI, because we don't need them on the daemon anymore.
 - If the GUI is not connected, the connections will be added to the
   backlog as in the point 2).
 - When the backlog reaches the limit, it'll keep deleting the last
   one in order to insert a new one.
 - The number of connections to keep on the backlog is configurable.
 - If the statistics configuration is missing, default values will be
   150 (maxEvents) and 25 (maxStats).

Notes:
 If the GUI is saving the data to memory (default), there won't be
 any noticeable side effect.
 If the GUI is configured to save the connections to a DB on disk, and
 the daemon sends all the backlog at once, the GUI may experience a
 delay and a high CPU spike. This can occur on connecting to the daemon
 (because the backlog will be full), or when an app sends too many
 connections per second (like nmap).
2021-08-13 12:18:10 +02:00
Gustavo Iñiguez Goia
38d4fb9ae5 Bump versions to v1.4.0rc4 2021-08-11 17:16:56 +02:00
Gustavo Iñiguez Goia
5a6dd28072 cache of PIDs: added new tests, fixed rare crash
- New tests to validate deleion of PIDs.
- Fixed potential out-of-bounds when deleting PIDs from cache.
2021-08-11 14:19:43 +02:00
Gustavo Iñiguez Goia
ba7c4e1878 allow to configure what firewall to use
Before this change, we tried to determine what firewall to use based on
the version of iptables (if -V legacy -> nftables, otherwise iptables).

This caused problems (#455), and as there's no support yet for nftables
system firewall rules, it can't be configured to workaround these
errors.

Now the default firewall to use will be iptables.
If it's not available (installed), can't be used or the configuration
option is empty/missing, we'll use nftables.
2021-08-09 00:32:27 +02:00
Gustavo Iñiguez Goia
9a34665d0e improved PID discovering
One of the steps of PIDs discovering is knowing what's the socket inode
of a connection. The first try is to dump the active connections in the
kernel, using NETLINK_SOCK_DIAG via netlink.

Sometimes when a source port was reused, the kernel could return multiple
entries with the same source port, leading us to associate connections with
the wrong application.

This change fixes this problem, while allowing us to discover other
apps.
More information:
https://github.com/evilsocket/opensnitch/issues/387#issuecomment-888663121

Note: this problem shouldn't occur using the procs monitor method eBPF.
2021-07-29 15:40:25 +02:00
Gustavo Iñiguez Goia
fbcab5a0c4 cache, pids/inodes: fixed race conditions, improvements
- Fixed multiple race conditions when using the cache of PIDs.
- Improved the chances to hit the cache of inodes, which helps to keep
  down the times to get the PID of a connection to <= 30us.

These caches are mainly used when not using "ebpf" proc monitor method.
2021-07-21 15:04:23 +02:00
Gustavo Iñiguez Goia
708a7e6333 Bump versions to v1.4.0rc3 2021-07-16 23:41:02 +02:00
Gustavo Iñiguez Goia
cec619aa3b removed debugging log message 2021-07-16 23:24:52 +02:00
Gustavo Iñiguez Goia
85802811d1 fixed race condition counting lists of domains
Removed the number of loaded lists of domains, that caused a race
condition and added no value. It was only used for debugging
purposes.
2021-07-07 18:50:30 +02:00
Gustavo Iñiguez Goia
21c6a914d6 drop connections while a pop-up is running
Prior to v1.4.x versions, when a pop-up asked the user to allow or deny
a connection, the rest of the network traffic was dropped until an
action was taken.

We fixed it, but when a pop-up was asking to allow or deny a new connection,
we let it passing by if the daemon's DefaultAction option was set to
allow, even if the user hadn't taken an action on it yet.

It also caused some confusion if the users had configured the pop-up's
DefaultAction to deny, they were expecting to not allow the connection
until they had decided what to do.

Now the previous behaviour has been restored, having these usage
scenarios:
- If the GUI is connected + daemon DefaultAction set to allow or deny.
  Result:
    1. Prompt the user to allow or deny the new connection.
    2. Deny the new connection until the user takes an action on it.
    3. Allow the rest of traffic, allowing known connections, and
       denying new ones until the active pop-up is closed and we can
       prompt the user again.

- GUI disconnected.
  Result:
    1. Apply daemon's DefaultAction from the configuration file
       default-config.json.

closes: #392
2021-06-16 09:50:36 +02:00
Gustavo Iñiguez Goia
465d531496 find PID: use legacy methods if the PID is not found.
Some times, processes that establish connections to localhost are only
found in /proc/net/* files. So if we fail to get the PID of a
connection, fallback to legacy method to find it.
2021-06-08 14:11:19 +02:00
Gustavo Iñiguez Goia
f143107639 ebpf: fixed race condition when listing connections
Better fix for locking already established connections.
2021-06-07 22:55:08 +02:00
Gustavo Iñiguez Goia
da23c827f7 added nftables support
Added basic nftables support, which adds the needed rules to intercept
outgoing network traffic and DNS responses. System rules will be added
soon.

What netfilter subsystem to use is determined based on the following:
- nftables: if the _iptables_ binary is not present in the system, or
  if the iptables version (iptables -V) is
  "iptables vX.Y.Z (nf_tables)".
- iptables: in the rest of the cases.
2021-06-07 01:32:05 +02:00
Gustavo Iñiguez Goia
3d11134363 fixed race condition when deleting a rule from disk 2021-06-05 16:33:50 +02:00
Gustavo Iñiguez Goia
ff40e99f77 tests, rules: added Duration change test
Verify that if the user changes the Duration of a temporary rule, the
old timer does not delete the rule.
2021-06-03 17:35:48 +02:00
Gustavo Iñiguez Goia
403c218209 prevent deleting temporary rules when the Duration changes
When the Duration of a rule changed (from 1h to 5m, from 5m to until
restart, etc), the timer of the old rule was fired, causing deleting the
rule from the list.

This erroneous behaviour could be one of the reasons of #429
2021-06-01 23:52:02 +02:00
Gustavo Iñiguez Goia
e5b54f0a6b eBPF: ignore netlink errors if there're no connections
When enabling the eBPF monitor method we dump the active connections,
but in some cases there're no active connections, and because of this
we're failing enabling this monitor method.

If there're no connections established, netlink returns 0 entries. It's
not clear if it's an indication of error in some cases or the expected
result.

Either way:
- fail only if we're unable to load the eBPF module.
- dump TCP IPv6 connections only if IPv6 is enabled in the syste,-
2021-05-29 00:16:18 +02:00
Gustavo Iñiguez Goia
1db03b5570 eBPF procs monitor method: fixed race conditions
It'd probably be a good idea to write a module and encapsulate all the
functionality of the fields in funcs(), to lock them properly
(get/set maps, etc).

TODO: replace monitorLocalAddress() by
netlink.AddrSubscribeWithoptions(), to receive addresses' events
asynchronously.
2021-05-26 22:52:33 +02:00
Gustavo Iñiguez Goia
60414931d6 when deleting a rule stop lists of domains monitor 2021-05-25 22:21:05 +02:00
Gustavo Iñiguez Goia
259a02026c
Merge pull request #417 from raboof/add-gobpf-to-go.mod
Add gobpf to go.mod
2021-05-19 21:51:24 +02:00
Arnout Engelen
356428b6c9
Add gobpf to go.mod
gobpf was introduced in #397 but not added to go.mod yet
2021-05-19 21:21:27 +02:00
Gustavo Iñiguez Goia
6ea63f72dc make "linux kernel" connections configurable
Sometimes when a new connection is about to be established, we don't get
the PID of the process using the eBPF proc monitor method. But in some
rare situations, the kernel still holds information about the connection
(sock_diag struct basically). We assume that these connections are
initiated from kernel space.

Per some debugging, this doesn't seem to be always the root cause, so
these connections will only be shown if InterceptUnknown config field is
set to true.
2021-05-16 23:58:28 +02:00
Gustavo Iñiguez Goia
423dbd6ff3 deb,rpm: minor changes to ease packing 2021-05-16 23:55:34 +02:00
Gustavo Iñiguez Goia
2e42e989d4 Bump versions to 1.4.0rc2
As we've added eBPF interception method, we need go iovisor ebpf package,
which is not packaged for Debian yet, so the way I was compiling it
differs a little bit (instead of using gbp buildpackage,
dpkg-buildpackage is used).

Aside from that, there'll be a new eBPF module (.o ELF), which must be
packaged with the packages. Will be compiled on the fly, but maybe it
could be hosted in the repo, because it won't change that much.
2021-05-07 21:24:42 +02:00
Gustavo Iñiguez Goia
0f7e93acdc fixed parsing /proc/$pid/stat starttime field
On systems that have been running for a long time (for example 552
days) we were failing parsing the starttime field:

```
Could not find or convert Starttime. This should never happen.
Please report this incident to the Opensnitch developers:
strconv.Atoi: parsing "4242026842": value out of range
```

- extra: fixed tests.
2021-04-20 16:31:49 +02:00
Gustavo Iñiguez Goia
966342cbbc ui: allow to stop/start interception from systray
- removed unused import time.
- allow to pause/start interception from tray contextual menu.
- improved case when the daemon is in paused state, and the GUI is
  launched.

There's more work yet to do to improve the states when there're several
nodes connected.

closes #398
2021-04-17 22:09:16 +02:00
Gustavo Iñiguez Goia
4332469fc2 improved nodes connectivity handling
problem:
 - after losing network connectivity node<->server, the node didn't restore
   the connection. In reality, the connection with the server was not
   closed, but the notifications channel was closed due to inactivity
   after 20s.

set inactivity timeouts to 20s on both node and server. Previous
timeouts were 2h for the main connection and 20s for the streaming
channels (notifications).

- get rid of the logic to determine if the server is alive or not based
  on sending pings.
  Instead, use the connection events when a node connects/disconnects
  (Subscribe).
  The Ping call is still used to send the statistics.

other:
- fixed exception when updating the status of a node.
2021-04-11 20:55:14 +02:00
Gustavo Iñiguez Goia
0a12fc5c73 don't save daemon config if monitor method fails
If we change the monitor method from the GUI, and it fails to start
(eBPF, audit or ftrace), use the old configured method and don't save
the configuration to disk, to avoid configuring a monitor method that
doesn't work.
2021-04-05 13:18:19 +02:00
Gustavo Iñiguez Goia
8ea58ade19 fixed ebpf error logging
+ split functionality into different files.
2021-04-05 12:19:00 +02:00
themighty1
9497cf8394
Use ebpf program to find PID of new connections. (#397)
* Use ebpf program to find PID of new connections.

    before running the branch you have to compile ebpf_prog/opensnitch.c
    opensnitch.c is an eBPF program. Compilation requires getting kernel source.

    cd opensnitch
    wget https://github.com/torvalds/linux/archive/v5.8.tar.gz
    tar -xf v5.8.tar.gz
    patch linux-5.8/tools/lib/bpf/bpf_helpers.h < ebpf_prog/file.patch
    cp ebpf_prog/opensnitch.c ebpf_prog/Makefile linux-5.8/samples/bpf
    cd linux-5.8 && yes "" | make oldconfig && make prepare && make headers_install # (1 min)
    cd samples/bpf && make
    objdump -h opensnitch.o #you should see many section, number 1 should be called kprobe/tcp_v4_connect
    llvm-strip -g opensnitch.o #remove debug info
    sudo cp opensnitch.o /etc/opensnitchd
    cd ../../../daemon

    --opensnitchd expects to find opensnitch.o in /etc/opensnitchd/
    --start opensnitchd with:

    opensnitchd -rules-path /etc/opensnitchd/rules -process-monitor-method ebpf

Co-authored-by: themighty1 <you@example.com>
Co-authored-by: Gustavo Iñiguez Goia <gooffy1@gmail.com>
2021-04-05 11:28:16 +02:00
Gustavo Iñiguez Goia
148526e527
Merge pull request #381 from raboof/support-more-recent-protoc-gen-go
Support more recent protoc-gen-go
2021-04-03 18:10:18 +02:00
Gustavo Iñiguez Goia
e2be2b769a improved monitor method switching (audit)
when switching from audit to another one.
2021-03-30 01:13:02 +02:00
Gustavo Iñiguez Goia
4b0b8ccef0 display an error if a monitor method has not been applied
Sometimes ftrace monitor method fails to apply, but we displayed on the
GUI that it had been applied.
2021-03-29 17:59:09 +02:00
Gustavo Iñiguez Goia
879d18f15a ui: fixed monitoring processes details
- ui: fixed error getting the icon of an app.
- ui: fixed getting the list of pids of an app.
- ui: improved proc details start/stop icon behaviour.
- daemon: improved error message when we fail getting the details of a
  process.

others:
 - changed icon search by system-search.
2021-03-29 01:36:41 +02:00
Arnout Engelen
1f26f66e8a
Support more recent protoc-gen-go
When building the project with protoc-gen-go version 1.5.1,
it fails with the following:

```
protoc -I. ui.proto --go_out=plugins=grpc:../daemon/ui/protocol/
protoc-gen-go: unable to determine Go import path for "ui.proto"

Please specify either:
	• a "go_package" option in the .proto source file, or
	• a "M" argument on the command line.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.
```

This can be fixed by adding the full go package as an option in the
proto file. To make sure the code is generated to the correct path,
we also have to add add the `paths=source_relative` option to the
protoc plugin.

After this, the code is generated correctly, but the generated code
references classes like grpc.ClientConnInterface which were introduced
in 1.27.0.
2021-03-28 16:38:21 +02:00
Gustavo Iñiguez Goia
8af290e970 Bump versions to 1.4.0rc1 2021-03-26 00:59:16 +01:00
Gustavo Iñiguez Goia
6048b0ef64 more cache improvements
- don't clean cache by number of items.
- clean inodes from cache every 2' if the descriptor symlink doesn't exist
  anymore, or if the lastSeen time is more than 5 minutes.
- launch cache cleaners before start a new process monitoring method,
  and start it only once for the life time of the daemon.
- do not store in cache the Time objects, only the nanoseconds of
  the last updated time.
- if the inode of a connection is found in cache, reorder the
  descriptors to push the descritptor to the top of the list.
  Also add cached the inode.

It turns out that when a new connection is about to be established,
when the process resolves the domain, the same inode is used to open the
tcp connection to the target. So if it's cached we save CPU cycles.

This also occurs when we block a connection and the process retries it,
or when a connection timeouts and the process retries it
(telnet 1.1.1.1).
2021-03-24 01:16:47 +01:00
Gustavo Iñiguez Goia
7b9a57b788 added -version cli option
closes #374
2021-03-22 17:51:31 +01:00
Gustavo Iñiguez Goia
630e3717dd auditd: cache new Proc objects 2021-03-21 21:55:59 +01:00
Gustavo Iñiguez Goia
36cdb76113 prevent leaks when reloading domains lists in debug
If the log level was debug, when reloading the lists of domains could
lead to memory leaks.
2021-03-21 21:16:57 +01:00
Gustavo Iñiguez Goia
1a61a2dca9 cache improvements
- update the descriptors/inodes of a PID when it's found in cache.
- when a descriptor/inode is found in cache, push it to the top
  of the descriptors list. The next time it's found in cache it'll be in
  the 1st position of the list, saving CPU time.
- added test cases and benchmark helpers to help analyzing performance.
2021-03-19 19:05:45 +01:00
Gustavo Iñiguez Goia
53904cb70f ignore hidden files when loading lists of domains 2021-03-14 20:23:48 +01:00
Gustavo Iñiguez Goia
0d2e8b56ea added operator tests
fixed lists operator tests.
added domain lists tests.
2021-03-05 01:10:01 +01:00
Gustavo Iñiguez Goia
408150073a fix deadlock
closes #365
2021-03-04 12:10:53 +01:00