apparmor: align quoting with libapparmor - #44
Conversation
e6c37cd to
aed3cd0
Compare
aed3cd0 to
d9e227e
Compare
7be3f28 to
e30519d
Compare
|
I think profile declarations and peer patterns need different escaping here. Opened thaJeztah#1 for consideration |
62df3d8 to
afd0b8f
Compare
|
Making some extra changes inspired by your "let's also check what apparmor_parser says about this" - I'll give you a nudge when done |
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Commit 47d6e85 updated the template to quote profile names. While the manpage ([apparmor.d(5)]) documents quoted names, it is unclear how quoting must be handled: PROFILE NAME = UNQUOTED PROFILE NAME | QUOTED PROFILE NAME QUOTED PROFILE NAME = '"' UNQUOTED PROFILE NAME '"' UNQUOTED PROFILE NAME = (must start with alphanumeric character (after variable expansion), or '/' AARE have special meanings; see below. May include VARIABLE. Rules with embedded spaces or tabs must be quoted.) The lexer in [parser/parser_lex.l] further describes the syntax of quoted identifiers: ALLOWED_QUOTED_ID [^\0"]|\\\" QUOTED_ID \"{ALLOWED_QUOTED_ID}*\" However, quoting alone does not preserve a profile name literally. [processunquoted] decodes recognized escape sequences and preserves escaping for AARE special characters so that they remain literal when processed by the pattern-matching backend. In particular, backslashes must be escaped to prevent sequences such as `\n` from being decoded, and AARE special characters such as `*`, `?`, `[]`, and `{}` must be escaped to prevent them from acquiring pattern semantics. See [processunquoted] and [strn_escseq]. Update profile-name quoting to account for both AppArmor escape sequences and AARE special characters, preserving the profile name literally. NUL values are not handled, as they are not valid in quoted identifiers and should not reach this code. Also update profileData to provide accessors for its fields, allowing profile names to be quoted by those methods instead of exposing raw values directly to the template. This also fixes handling of the `unconfined` daemon profile. The previous quoting change rendered it as `"unconfined"`, which changes its AppArmor semantics from the special unconfined selector to a literal profile-name expression. Keep this sentinel unquoted while continuing to quote actual profile names. Updates 47d6e85 [apparmor.d(5)]: https://manpages.ubuntu.com/manpages/xenial/man5/apparmor.d.5.html [parser/parser_lex.l]: https://gitlab.com/apparmor/apparmor/-/blob/v5.0.2/parser/parser_lex.l#L286-287 [processunquoted]: https://gitlab.com/apparmor/apparmor/-/blob/v5.0.2/parser/parser_misc.c#L468-507 [strn_escseq]: https://gitlab.com/apparmor/apparmor/-/blob/v5.0.2/parser/lib.c#L178-218 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
afd0b8f to
ad1e7de
Compare
| { | ||
| name: "with-special-characters", | ||
| data: profileData{ | ||
| name: `foo"bar,*?[ab]{c,d}^\baz`, | ||
| daemonProfile: `daemon"bar,*?[ab]{c,d}^\baz`, | ||
| }, | ||
| }, |
There was a problem hiding this comment.
Added a test-case here as well, so that the before/after is more apparent. Test passes "before", but because it just takes the quoted profile as a name.
| @{PROC}=/proc/ | ||
|
|
||
| profile "foo\"bar\,\*\?\[ab\]\{c\,d\}\^\\baz" flags=(attach_disconnected,mediate_deleted) { | ||
| profile "foo\"bar,*?[ab]{c,d}^\baz" flags=(attach_disconnected,mediate_deleted) { |
There was a problem hiding this comment.
And this shows the diff clearly before/after 👍
| // Declarations retain AARE escapes, while peer patterns consume them. | ||
| for _, want := range []string{ | ||
| `profile "foo\"bar,*?[ab]{c,d}^\baz" flags=(attach_disconnected,mediate_deleted) {`, | ||
| ` signal (receive) peer="daemon\,profile\\baz",`, | ||
| ` signal (send,receive) peer="foo\"bar\,\*\?\[ab\]\{c\,d\}\^\\baz",`, | ||
| ` ptrace (trace,tracedby,read,readby) peer="foo\"bar\,\*\?\[ab\]\{c\,d\}\^\\baz",`, | ||
| } { | ||
| if !strings.Contains(out.String(), "\n"+want+"\n") { | ||
| t.Errorf("generated profile is missing line %q", want) | ||
| } | ||
| } |
There was a problem hiding this comment.
Oh, perhaps this is redundant now, because I added it to the test-table 🤔
There was a problem hiding this comment.
I removed this one 😅
(but probably we could move the test-table to a non-linux file, or just make it non-linux; most tests should be testable cross-platform)
AARE escaping changed the declared profile name. Keep declaration quoting separate from peer-pattern escaping. Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
ad1e7de to
f0494f1
Compare

apparmor: splitCon: touch-up GoDoc
apparmor: align profile-name quoting with libapparmor
Commit 47d6e85 updated the template to
quote profile names. While the manpage (apparmor.d(5)) documents quoted
names, it is unclear how quoting must be handled:
The lexer in parser/parser_lex.l further describes the syntax of quoted
identifiers:
However, quoting alone does not preserve a profile name literally.
processunquoted decodes recognized escape sequences and preserves
escaping for AARE special characters so that they remain literal when
processed by the pattern-matching backend. In particular, backslashes
must be escaped to prevent sequences such as
\nfrom being decoded,and AARE special characters such as
*,?,[], and{}must beescaped to prevent them from acquiring pattern semantics. See
processunquoted and strn_escseq.
Update profile-name quoting to account for both AppArmor escape sequences
and AARE special characters, preserving the profile name literally. NUL
values are not handled, as they are not valid in quoted identifiers and
should not reach this code.
Also update profileData to provide accessors for its fields, allowing
profile names to be quoted by those methods instead of exposing raw
values directly to the template.
This also fixes handling of the
unconfineddaemon profile. The previousquoting change rendered it as
"unconfined", which changes its AppArmorsemantics from the special unconfined selector to a literal profile-name
expression. Keep this sentinel unquoted while continuing to quote actual
profile names.
Updates 47d6e85