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

snap/naming: add validator for snap security tag #8408

Merged
merged 6 commits into from Apr 7, 2020

Conversation

zyga
Copy link
Collaborator

@zyga zyga commented Apr 2, 2020

Security tags were only created by snapd, never read from external
sources. With the upcoming refresh app awareness patches we will have to
parse and validate security tags that are embedded in cgroup names.

This patch provides the validator. It was cherry-picked from the main
feature branch.

Signed-off-by: Zygmunt Krynicki me@zygoon.pl

Security tags were only created by snapd, never read from external
sources. With the upcoming refresh app awareness patches we will have to
parse and validate security tags that are embedded in cgroup names.

This patch provides the validator. It was cherry-picked from the main
feature branch.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
@jdstrand jdstrand self-requested a review April 2, 2020 17:05
@zyga zyga added the Skip spread Indicate that spread job should not run label Apr 2, 2020
snap/naming/validate.go Outdated Show resolved Hide resolved
snap/naming/validate.go Show resolved Hide resolved
zyga added 2 commits April 3, 2020 15:10
When we are validating security tags we don't need to split the tag over
each dot, only over the first four we expect to see.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
When checking security tags we can look at the fixes bits ("snap" and
"hook") before we even attempt to look at the variable parts.

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
Copy link
Collaborator

@bboozzoo bboozzoo 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 for the updates.

Copy link

@jdstrand jdstrand left a comment

Choose a reason for hiding this comment

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

LGTM. There are a couple of observations but nothing blocking.

// invalid number of components are rejected.
c.Check(naming.ValidateSecurityTag("snap.pkg.hook.surprise."), ErrorMatches, "invalid security tag")
c.Check(naming.ValidateSecurityTag("snap.pkg.hook."), ErrorMatches, "invalid security tag")
c.Check(naming.ValidateSecurityTag("snap.pkg.hook"), IsNil) // surprise, it is a valid app name!
Copy link

Choose a reason for hiding this comment

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

Nitpick (not a blocker) perhaps you don't want to use 'surprise' here since you use 'surprise' up above to indicate an error? Perhaps just // actually a valid app name! :) or similar?

//
// TODO: handle the weird udev variant.
func ValidateSecurityTag(tag string) error {
parts := strings.SplitN(tag, ".", 4)
Copy link

Choose a reason for hiding this comment

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

The SplitN() change is fine but I actually preferred Split() since SplitN(..., 4) with something that would've had more parts forces the additional check in the ValidateApp()/ValidateHook() rather than erroring early off the simple length check. Not a blocker, just an observation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed, I'll change it to SplitN(..., 5) so that the length check remains an early warning and add a comment as to why.

}
if err := ValidateInstance(snapName); err != nil {
return errInvalidSecurityTag
}
Copy link

Choose a reason for hiding this comment

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

Again, not a blocker, but you could slightly refactor and put the snapLiteral and ValidateInstance() checks before the switch since they are identical for both case 3 and 4 (you'd then obviously have to check the length is at least 2 though).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea, applied.

zyga added 3 commits April 7, 2020 12:35
Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
Thanks to Jamie for the idea :)

Signed-off-by: Zygmunt Krynicki <me@zygoon.pl>
@mvo5 mvo5 merged commit ec695a8 into snapcore:master Apr 7, 2020
@zyga zyga deleted the feature/validate-security-tag branch April 8, 2020 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Skip spread Indicate that spread job should not run
Projects
None yet
4 participants