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

WIP: Psp rules support #682

Closed
wants to merge 97 commits into from
Closed

WIP: Psp rules support #682

wants to merge 97 commits into from

Conversation

mstemm
Copy link
Contributor

@mstemm mstemm commented Jun 20, 2019

What type of PR is this?

Uncomment only one /kind <> line, hit enter to put that in a new line, and remove leading whitespaces from that line:

/kind bug
/kind cleanup
/kind design
/kind documentation
/kind failing-test
/kind feature
/kind flaky-test

If contributing rules or changes to rules, please make sure to uncomment the appropriate kind

/kind rule/update
/kind rule/create

Any specific area of the project related to this PR?

/area engine
/area rules

/area deployment
/area integrations
/area examples

What this PR does / why we need it:

We've heard from some users that it would be desirable to have a way to "test" a K8s Pod Security Policy (PSP) to see if it has good coverage, before actually enabling the PSP. Once a PSP is in place, it can prevent pods from running, which can be disruptive.

This adds support for reading a K8s Pod Security Policy (PSP) and converting it to a set of falco rules. These rules can check the conditions specified in the PSP without actually preventing pods from running. This could be a valuable step in development of PSPs.

This adds new command line options --psp <path> and --psp_save [<path>]. When a psp is provided using --psp, it will be internally converted to a set of falco rules that check the conditions of the PSP and loaded into the falco engine. If you wish to save the set of converted falco rules, you can save them using --psp_save.

Many of these rules depend on K8s Audit support, so you'll need to enable and configure K8s Audit in your cluster to fully test a PSP.

Which issue(s) this PR fixes:

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

Add support for reading K8s Pod Security Policies (PSPs) and converting them to a set of falco rules. This can be used to test PSPs without the risk of blocking pod deployments.

@poiana
Copy link

poiana commented Jun 20, 2019

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: mstemm

If they are not already assigned, you can assign the PR to them by writing /assign @mstemm in a comment when ready.

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@leodido
Copy link
Member

leodido commented Jun 20, 2019

/kind rule-create

@leodido
Copy link
Member

leodido commented Jun 20, 2019

@mstemm thanks!

It needs a rebase on top of the master to catch the falco fields checksum, but I'm sure you already know this 😄

Add a new item that can be in brackets for a field: port ranges. They
are a mix of digits, ':', and ','. The current grammar allows any mix
without strictly checking pairs of ports separated by : and joined by ,.
ka.req.container.host_{pid,ipc}: returns value of host{PID,IPC} for pod
spec.

ka.req.container.host_port.within: given a list of port ranges expressed
as "min:max,min2:max2", return true if all ports specified for a pod are
within those ranges. The work is in
k8s_audit_filter_check::check_host_port_within which iterates over the
containers to find each hostPort and match against the ranges.
Matches everything currently defined in the template.
A working template for the following PSP properties:

 - privileged
 - hostPID
 - hostIPC
 - hostNetwork
 - hostPorts
New field ka.req.volume_types.within checks the set of volume types
against a list of allowed types and returns true if all volume types are
in the allowed list.
Compares volume types against an allowed set.
Used for a comma-separated list of paths.
This will be easier once we're getting the values from yaml.
ka.req.volume.any_hostpath is identical to ka.req.volume.hostpath,
seeing if any hostPath path matches the provided prefix. (You can now
specify a list of prefixes separated by commas).

ka.req.volume.all_hostpath checks if *all* hostPaths match the provided
set of prefixes.
Uses ka.req.volume.all_hostpath to ensure that all hostPath paths match
the provided allowed set of paths.
When rules are enabled in the falco engine, the argument to
enable_rule() is a regex pattern. That causes problems if the rule name
itself contains regex characters like '(', etc.

To fix this, escape special characters in rule names to create the
pattern passed to enable_rule().

Signed-off-by: Mark Stemm <mark.stemm@gmail.com>
New test has a rule name containing all ESCMAScript regex chars and
ensures that the rule matches the events.

The test code itself needed to also escape special characters from
patterns when searching falco's output.

Signed-off-by: Mark Stemm <mark.stemm@gmail.com>
Add rules for readOnlyRootFilesystem, runAsUser + MustRunAs, runAsUser +
MustRunAsNonRoot, and runAsGroup + MustRunAs.
Add tests for readOnlyRootFilesystem, runAsUser+MustRunAs,
runAsUser+MustRunAsNonRoot, runAsGroup+MustRunAs.
Json event filterchecks now extract an array of values instead of a
single string value. This allows changing a lot of filter checks that
were really performing set comparison operators
ka.req.container.host_port.within, ka.req.volume.any_hostpath, etc to
just perform set comparison operations.

This also changes json_event_filter_check::compare to work primarily on
sets instead of single values. CONTAINS/IN look for subsets between the
extracted and rhs values, and a new operator INTERSECTS looks for any
set intersection.

Aliases now have separate steps for custom extraction from a json object
to an array of strings (things like plucking custom values from an array
of objects, etc) and indexing. Most indexing should be eliminated and
replaced with (simpler) custom extraction.

The aliases themselves still need to be changed to work with extraction
+ indexing functions instead of a single formatting function.
Allow nested/recursive navigation of the object by default, start of
general-purpose extracted values which can be strings, ints, or pairs of
ints that act as ranges.
This is mostly complete and removes unnecessary filterchecks that were
actually working as comparison operators. Remaining work:

1. Finish implementing the default extraction function
2. Update templates to switch over to set/equality operators instead of
   old filterchecks
3. Get build working
4. Fix automated tests
This version works, but there's a mismatch in how m_field is handled
when extracting and resolving tokens.
No tests result in errors any longer. 20/74 tests are still failing, though.
The remaining failures are host path matching against prefixes.
Some regressions in the normal k8s audit unit tests, though. I think I
just need to adapt some of the new filter fields to match their new semantics.
Instead of a hard-coded image list, use an annotation on the psp object
"falco-rules-psp-images".
@poiana
Copy link

poiana commented Sep 9, 2019

Thanks for your pull request. Before we can look at it, you'll need to add a 'DCO signoff' to your commits.

📝 Please follow instructions in the contributing guide to update your commits with the DCO

Full details of the Developer Certificate of Origin can be found at developercertificate.org.

The list of commits missing DCO signoff:

  • b94c035 WIP on yaml rules templating
  • 773d4b4 More WIP on rule templating
  • 5443a61 Add a new index for port ranges
  • 8452feb Add new k8s audit fields for psp policies
  • 5bfe1e4 Fill in sample values for psp policy knobs
  • d18f839 Functional template for some fields
  • 76d0817 New field ka.req.volume_types.within for PSPs
  • 68f01a7 Add support for volume types
  • cd4c89b Allow commas in path spec
  • c61e032 Make allowed volume types a real list
  • 707a9d3 Add addl ways to match host paths
  • e39fbc2 Template for allowed host paths
  • 393cd28 WIP on PSP (flexvolume drivers)
  • d3850a6 WIP PSP (fsGroup)
  • aec9e8a PSP WIP (readOnlyRootFilesystem)
  • 4cee80f WIP PSP readOnlyRootFilesystem
  • 6e69220 Remove debug prints
  • e932bea PSP WIP runAsUser/runAsNonRoot
  • 9691ac3 WIP PSP runAsGroup
  • e475801 Add missing defn.
  • 1731ea6 PSP WIP supplementalGroups
  • 82164f9 WIP PSP allowPrivilegeEscalation
  • c88dc1b WIP PSP (allowed capabilities)
  • 4a97f69 WIP PSP allowedProcMountTypes
  • 71b74e4 Be consistent about camelCase (e.g. don't use it)
  • 19a333d WIP PSP get yaml parsing of psp working
  • d552d1d WIP PSP add inja to build
  • c7f8f9c Update engine version checksum
  • 44757cb First version working w/ inja templates
  • 47329f4 Store params as json object directly
  • 9da22b8 Optionally save psp generated rules
  • d22082d Cleaning up template handling
  • 55c1e7b More template cleanups
  • 15eafb8 Finish template formatting changes.
  • 5ef8cd1 Fix falco compilation problems w/ generated rules
  • 2d8c38e Fix remaining logical errors w/ templates
  • f03c88b Update fields checksum
  • 3626620 Initial skeleton for PSP tests
  • 15f8eca Add test case for hostPID
  • 3bb2019 Don't save psp rules by default.
  • 1354378 Add test for PSP hostIPC
  • d780fa8 Update name for test host pid psp
  • f9f4b94 Tests for PSP HostNetwork
  • 1c783f3 Small wording change in template
  • 7005ff4 Test for PSP HostPorts
  • 42fe909 Fix logic for ranges
  • 59ac24a Test for PSP Volumes
  • 327fd68 Test PSP Allowed Host Paths
  • 966b522 Small template formatting change
  • 127ce70 Tests PSP FlexVolume drivers
  • 32f98a4 Test PSP fs group + MustRunAs
  • 61e1ed2 Add support for >,>=,<,<= operators
  • 6722d83 PSP Test MayRunAs
  • 17b74e9 Test PSP fsGroup + RunAsAny
  • 5c54cf9 Use right value for "not exists"
  • 66386b4 Properly parse RunAsAny for some psp params
  • a8c5d05 Test PSP readOnlyRootFilesystem + neg cases
  • 493debb Allow no ports/volumes for port/volume checks
  • 3dd6156 WIP on tests for runasuser
  • b7f9bb4 Add new fields for matching items against any vals
  • b66834c Add note to self for more testing.
  • 43b631f Refactoring/simplifying runAsUser/runAsGroup chks
  • a4f6e34 Get current runAsUser tests passing
  • 2152609 Use right rule for runAsRoot test
  • 81d67de Complete runAsUser tests
  • 04aed8e Tests for PSP runAsGroup
  • 3b3ee9e Fill in must_run_as group indexing methods
  • 0dfc084 Tests for PSP group mayRunAs
  • d8ae8c8 Rule name tweaking.
  • 7d142d3 Tests PSP supplementalGroups + MustRunAs
  • 73b5e66 Fixes for supplementalGroups + MustRunAs
  • 6be891f PSP test supplemental + MayRunAs
  • a5dec7e Tests for PSP + allowPrivilegeEscalation
  • 40bdb83 Fix tests for PSP allowPrivilegeEscalation
  • 7264ae2 Add tests for allowedCapabilities
  • 0f5951f Fix tests for PSP allowedCapabilities
  • e839288 Add PSP Tests for allowedProcMount types
  • 904313e Fix PSP tests for allowedProcMount types
  • 79f05e6 WIP on rule renaming
  • 2c6b5e7 Allow --psp with no rules files
  • 8fad12f Fix bugs in rules specs
  • d0263ca Add addl syscall based rules
  • 6f9cb99 Add tests for new syscall based PSP rules
  • d8ba5d0 Fix most compile errors
  • 70d615a Get tests passing again after rebase.
  • 2641f52 WIP on refactoring json event filterchecks
  • 585fbb8 More WIP on alias refactoring
  • 3246c56 More WIP on cleanups
  • 6d26387 More WIP on aliases cleanup
  • e99605f This version passes 1/3 of the automated psp tests
  • bd0862f Passing more tests
  • 6f688cb All but 2 tests passing
  • 76ed934 This version passes all psp unit tests
  • 150df12 This version passes all unit tests, finally!
  • f3f22ad Use annotates to scope autognerated rules

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@mstemm mstemm closed this Sep 9, 2019
@mstemm mstemm deleted the psp-rules-support branch September 9, 2019 20:06
@mstemm
Copy link
Contributor Author

mstemm commented Sep 10, 2019

Replaced by #825 and #826

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants