Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite ports and CIDRs #1222

Merged
merged 7 commits into from
Jul 19, 2023
Merged

Conversation

kevsecurity
Copy link
Contributor

This series adds functionality to socket matching for sock and skb types. Previously, ports and CIDRs could be specified, but only a limited number would be accepted, due to complexity issues surrounding checking members of lists. This series uses an argfilter map to specify ports so that theoretically any number could be specified, and a new addr4filter map to specify IPv4 CIDRs, that are matched using a longest prefix match TRIE map.

In addition, ports can now be specified as ranges using the 'min:max' format, and new operators SPortPriv and DPortPriv were added to specify the range of privileged ports, from 0 to 1023 inclusive. On top of that, all port and address operators now can be prefixed with the Not keyword to create an inverse match, e.g. NotSAddr, and these can be combined with their positive equivalents to remove entires from a larger set.

@kevsecurity kevsecurity requested review from mtardy and a team as code owners July 13, 2023 14:01
@netlify
Copy link

netlify bot commented Jul 13, 2023

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit de5a841
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/64b6bc7c8c0a4f0008797dd8
😎 Deploy Preview https://deploy-preview-1222--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@kevsecurity kevsecurity force-pushed the pr/kevsecurity/infinite-ports-cidrs branch from 91aa4aa to f23cc64 Compare July 13, 2023 14:37
@kkourt kkourt self-requested a review July 13, 2023 15:23
Copy link
Contributor

@kkourt kkourt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I went over the first patch, and overall looks good to me!
Please find some comments below.

Most of them are nits, but the one that I'd like to be addressed if possible is checking the type of arguments and reject invalid policies (assuming that applies here).

As also discussed offline, it would be great if we could add some more tests for this new functionality.

pkg/selectors/kernel.go Outdated Show resolved Hide resolved
pkg/selectors/kernel.go Show resolved Hide resolved
pkg/selectors/kernel.go Outdated Show resolved Hide resolved
pkg/selectors/kernel.go Show resolved Hide resolved
pkg/selectors/kernel.go Outdated Show resolved Hide resolved
@kevsecurity kevsecurity force-pushed the pr/kevsecurity/infinite-ports-cidrs branch from f23cc64 to fd5dbf3 Compare July 17, 2023 12:22
@kevsecurity kevsecurity requested a review from kkourt July 17, 2023 12:22
@kevsecurity kevsecurity force-pushed the pr/kevsecurity/infinite-ports-cidrs branch from fd5dbf3 to 958d704 Compare July 17, 2023 14:28
@kevsecurity kevsecurity force-pushed the pr/kevsecurity/infinite-ports-cidrs branch from 958d704 to 3e197cc Compare July 17, 2023 14:38
The sock and skb types permit matching on port numbers. The port numbers
were stored as a list in the kernel selectors, which forced an upper
limit on the number of ports that could be specified.

This commit stores the specified ports in an argfilter map so that
theoretically any number of ports could be specified without exceeding
any obvious limits. The map is a hash of port numbers instead of a list.

In addition, all value types, including SPort and DPort can now take a
range, specified as min:max.

Signed-off-by: Kevin Sheldrake <kevin.sheldrake@isovalent.com>
For sock and skb types it is possible to match on source and destination
ports. This commit adds the ability to match on the source or
destinations ports not being in a list or range.

Signed-off-by: Kevin Sheldrake <kevin.sheldrake@isovalent.com>
The socket matchers for sock and skb types can match on a set or range
of specified ports, for both membership and non-membership of the set,
and for both source and destination ports. One obvious use case would be
to match on the set of privileged ports, from 0 to 1023 inclusive. As
this is a common, unchanging set, it makes sense to provide options to
match on this set without having to specify it as a range. This will
reduce the number of ports needed to be set in a map, will reduce the
number of argfilter maps, and will reduce the complexity in the BPF code
by a map lookup.

This commit introduces the SPortPriv, NotSPortPriv, DPortPriv and
NotDPortPriv operators that match the sock and skb ports to the
privileged set. They do not take a value argument.

Signed-off-by: Kevin Sheldrake <kevin.sheldrake@isovalent.com>
We currently supply a list of IPv4 CIDRs to match sock and skb types on.
This is limited to a small number due to complexity reasons. There is a
desire to be able to match on any number of CIDRs. This commit moves the
specification of the CIDRs to a LPM TRIE map which allows longest prefix
matching (which was designed for CIDR lookups). Any number of CIDRs can
now be specified.

Also, check that protocol is only specified for sock/skb types.

Signed-off-by: Kevin Sheldrake <kevin.sheldrake@isovalent.com>
Given that we can match on IPv4 CIDRs, it makes sense that we should
also be able to match on address not being in the specified ranges.
These Not matchers can be combined with the existing ones to exclude
individual addresses or sub-ranges from larger ranges.

For example, DAddr: 192.168.1.0/24 would match everything in the given
subnet. But combined with NotDAddr: 192.168.1.1 would exclude this
specific IP address from the match.

Alternatively, just using NotDAddr: 127.0.0.0/8 would exclude all
localhost traffic from the match.

Signed-off-by: Kevin Sheldrake <kevin.sheldrake@isovalent.com>
This commit adds documentation for matching socks and skbs.

Signed-off-by: Kevin Sheldrake <kevin.sheldrake@isovalent.com>
@kevsecurity kevsecurity force-pushed the pr/kevsecurity/infinite-ports-cidrs branch from 3e197cc to de5a841 Compare July 18, 2023 16:23
Copy link
Contributor

@kkourt kkourt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks! 🚀

bpf/process/types/basic.h Show resolved Hide resolved
pkg/sensors/tracing/kprobe_test.go Outdated Show resolved Hide resolved
This commit adds tests to confirm that the sock/skb operators are
working correctly. Limited the new tests to TCP sock only, because skb
works in exactly the same way and adding tests for UDP and/or skb would
only duplicate tests for little benefit.

Also, stop checking protocol on sock tests as this is unreliable.

Signed-off-by: Kevin Sheldrake <kevin.sheldrake@isovalent.com>
@kevsecurity kevsecurity force-pushed the pr/kevsecurity/infinite-ports-cidrs branch from de5a841 to 61c8eaf Compare July 19, 2023 09:09
@kevsecurity kevsecurity merged commit 087dba8 into main Jul 19, 2023
23 checks passed
@kevsecurity kevsecurity deleted the pr/kevsecurity/infinite-ports-cidrs branch July 19, 2023 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants