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

i/apparmor: add snippets with priorities #14061

Conversation

sergio-costas
Copy link
Contributor

AppArmor rules that forbid access to a resource have more priority than rules that allow access to those same resources. This means that if an interface restricts access to an specific resource, it won't be possible to enable access to that same resource from another, more privileged, interface.

An example of this is reading the .desktop files of all the installed snaps in the system: the superprivileged interface 'desktop-launch' enables access to these files, so any snap that has a connected plug for this interface should be able to read them. Unfortunately, the 'desktop-legacy' interface explicitly denies access to these files, and since it is connected automatically if a snap uses the 'desktop' or the 'unity7' interfaces, this mean that no graphical application will be able to read the .desktop files, even if the super- privileged interface 'desktop-launch' interface is connected.

To allow this specific case, a temporary patch (
#13933) was created and merged, but it is clearly an ugly and not-generic solution. For this reason, this new patch was created, following the specification https://docs.google.com/document/d/1K-1MYhp1RKSW_jzuuyX7TSVCg2rYplKZFdJbZAupP4Y/edit

This patch allows to add "prioritized snippets". Each one has an UID and a priority. If no prioritized snippet with the same UID has been previously added, the new prioritized snippet will be added like any other normal snippet. But if there is already an added snippet with the same UID, then the priority of both the old and the new snippets are compared. If the new priority is lower than the old one, the new snippet is ignored; if the new priority is bigger than the old one, the new snippet fully replaces the old one. Finally, if both priorities are the same, the new snippet will be appended to the old snippet.

This generic mechanism allows to give an interface priority over others if needed, like in the previous case.

Thanks for helping us make a better snapd!
Have you signed the license agreement and read the contribution guide?

@sergio-costas sergio-costas force-pushed the UDENG-3108-create-proposal-and-patch-for-app-armor-rule-priorization branch from 07ec35f to c2a031e Compare June 10, 2024 16:48
@sergio-costas sergio-costas changed the title Add snippets with priorities snippets: Add snippets with priorities Jun 10, 2024
@sergio-costas sergio-costas force-pushed the UDENG-3108-create-proposal-and-patch-for-app-armor-rule-priorization branch from c2a031e to 1fd1a38 Compare June 10, 2024 17:01
Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

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

Quick review to share my feelings.

I think the premise is right, some details on usability should be explored to make sure we're not making this readable for ourselves.

interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec_test.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
@sergio-costas sergio-costas force-pushed the UDENG-3108-create-proposal-and-patch-for-app-armor-rule-priorization branch from 8c93503 to 96ebe29 Compare June 11, 2024 10:40
@sergio-costas
Copy link
Contributor Author

Rebased to take advantage of the changes at #14032

@sergio-costas sergio-costas requested a review from zyga June 11, 2024 10:53
Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

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

A quick pass. I like almost everything. Small notes on the use of types - no need to have key.GetFoo that returns the string. Use the real type all the way.

Please remove uid from any comments and reword to mention keys.

Ping for a final pass.

+0.9

interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/connection.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
@sergio-costas sergio-costas requested a review from zyga June 11, 2024 11:40
@sergio-costas
Copy link
Contributor Author

sergio-costas commented Jun 11, 2024

I kept the SnippetKey type as a struct to ensure that a string can't be used instead in the functions (unless there's a way to forbid using a string instead of type SnippetKey string... this is: to avoid allowing spec.RegisterSnippetKey("akeyvalue") if SnippetKey type is derived from string).

@sergio-costas
Copy link
Contributor Author

(unless you think it's a good idea to allow it, of course...)

Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

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

Have a look.

I'm almost inclined to approve but please tweak the tests and let's get a +1/-1 on the panic behaviour from @pedronis

interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Show resolved Hide resolved
interfaces/connection.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec_test.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec_test.go Outdated Show resolved Hide resolved
@pedronis pedronis added the Needs Samuele review Needs a review from Samuele before it can land label Jun 18, 2024
@pedronis pedronis self-requested a review June 18, 2024 08:47
zyga
zyga previously approved these changes Jun 19, 2024
Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks :-)

@pedronis pedronis changed the title snippets: Add snippets with priorities i/apparmor: add snippets with priorities Jun 19, 2024
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

I'm not sure how we plan to use SnippetKey registration, where would we do the registration from, as it doesn't allow the re-register the same key

interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

I'm don't find Get... very useful, having a string inside the key is still useful for debugging though. A way to get all the keys would be useful though for tests

interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
@sergio-costas sergio-costas force-pushed the UDENG-3108-create-proposal-and-patch-for-app-armor-rule-priorization branch from 0e61400 to 49d8788 Compare July 2, 2024 11:33
@sergio-costas
Copy link
Contributor Author

@pedronis Rebased the code and re-implemented the priority between unity7/desktop-legacy and desktop-launch using the new mechanism.

@pedronis pedronis self-requested a review July 4, 2024 08:27
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

suggestion

interfaces/builtin/desktop_launch.go Outdated Show resolved Hide resolved
@pedronis pedronis self-requested a review July 5, 2024 13:00
@pedronis pedronis added this to the 2.64 milestone Jul 5, 2024
Copy link
Collaborator

@pedronis pedronis left a comment

Choose a reason for hiding this comment

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

thank you, couple of small comments

interfaces/builtin/desktop_launch.go Outdated Show resolved Hide resolved
interfaces/builtin/desktop_launch.go Outdated Show resolved Hide resolved
@pedronis pedronis dismissed zyga’s stale review July 8, 2024 09:36

last commits need a review

Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

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

LGTM with a few nitpicks on non-naming things and some on naming (apologies).

interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec_test.go Outdated Show resolved Hide resolved
interfaces/apparmor/spec_test.go Show resolved Hide resolved
interfaces/apparmor/spec_test.go Show resolved Hide resolved
sergio-costas and others added 3 commits July 8, 2024 16:04
AppArmor rules that forbid access to a resource have more
priority than rules that allow access to those same resources.
This means that if an interface restricts access to an specific
resource, it won't be possible to enable access to that same
resource from another, more privileged, interface.

An example of this is reading the .desktop files of all the
installed snaps in the system: the superprivileged interface
'desktop-launch' enables access to these files, so any snap
that has a connected plug for this interface should be able
to read them. Unfortunately, the 'desktop-legacy' interface
explicitly denies access to these files, and since it is
connected automatically if a snap uses the 'desktop' or the
'unity7' interfaces, this mean that no graphical application
will be able to read the .desktop files, even if the super-
privileged interface 'desktop-launch' interface is connected.

To allow this specific case, a temporary patch (
canonical#13933) was created and
merged, but it is clearly an ugly and not-generic solution.
For this reason, this new patch was created, following the
specification https://docs.google.com/document/d/1K-1MYhp1RKSW_jzuuyX7TSVCg2rYplKZFdJbZAupP4Y/edit

This patch allows to add "prioritized snippets". Each one has
an UID and a priority. If no prioritized snippet with the same
UID has been previously added, the new prioritized snippet will
be added like any other normal snippet. But if there is already
an added snippet with the same UID, then the priority of both
the old and the new snippets are compared. If the new priority
is lower than the old one, the new snippet is ignored; if the
new priority is bigger than the old one, the new snippet fully
replaces the old one. Finally, if both priorities are the same,
the new snippet will be appended to the old snippet.

This generic mechanism allows to give an interface priority
over others if needed, like in the previous case.
Co-authored-by: Zygmunt Bazyli Krynicki <[email protected]>
sergio-costas and others added 21 commits July 8, 2024 16:04
Co-authored-by: Zygmunt Bazyli Krynicki <[email protected]>
Co-authored-by: Zygmunt Bazyli Krynicki <[email protected]>
Co-authored-by: Zygmunt Bazyli Krynicki <[email protected]>
This creates a centralized key registry inside apparmor module,
so keys can be registered using top variables, and any
duplicated key will produce a panic when snapd is launched,
thus just panicking in any test too.
A previous PR was merged with a Quick&Dirty(tm) solution to the
priority problem between unity7 and desktop-legacy interfaces
against desktop-launch interface.

Now that it has been merged, that code must be updated to the
new mechanism implemented in this PR. This is exactly what this
commit does.
@sergio-costas sergio-costas force-pushed the UDENG-3108-create-proposal-and-patch-for-app-armor-rule-priorization branch from b321132 to e51e95b Compare July 8, 2024 14:04
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 93.68421% with 6 lines in your changes missing coverage. Please review.

Project coverage is 78.75%. Comparing base (ea13a33) to head (e51e95b).
Report is 37 commits behind head on master.

Files Patch % Lines
interfaces/apparmor/spec.go 94.73% 2 Missing and 1 partial ⚠️
interfaces/builtin/desktop_launch.go 86.36% 3 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #14061      +/-   ##
==========================================
+ Coverage   78.73%   78.75%   +0.02%     
==========================================
  Files        1055     1061       +6     
  Lines      138275   139262     +987     
==========================================
+ Hits       108866   109671     +805     
- Misses      22588    22739     +151     
- Partials     6821     6852      +31     
Flag Coverage Δ
unittests 78.75% <93.68%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ernestl ernestl merged commit c59a5f6 into canonical:master Jul 8, 2024
50 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Samuele review Needs a review from Samuele before it can land
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants