Skip to content

contrib/apparmor: fix whitespace handling in profile names#13278

Open
thaJeztah wants to merge 2 commits intocontainerd:mainfrom
thaJeztah:apparmor_fix_whitespace
Open

contrib/apparmor: fix whitespace handling in profile names#13278
thaJeztah wants to merge 2 commits intocontainerd:mainfrom
thaJeztah:apparmor_fix_whitespace

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

@thaJeztah thaJeztah commented Apr 23, 2026

contrib/apparmor: fix whitespace handling in profile names

The profile we read from /proc/self/attr/current will contain a newline,
resulting in a stray newline to be added to the profile;

Diff:
--- Expected
+++ Actual
@@ -16,3 +16,4 @@
   # Manager may send signals to container processes.
-  signal (receive) peer=unconfined,
+  signal (receive) peer=unconfined
+,
   # Container processes may send signals amongst themselves.

Trim whitespace to account for newlines and fix handling of whitespace as
AppArmor profile names are allowed to contain spaces when quoted; from the
apparmor.d(5) man-page:

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.)

While we don't use those names in our code, let's make the code correct.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes AppArmor profile generation by ensuring the daemon’s current profile name (read from /proc/self/attr/current) is sanitized to avoid trailing newlines/whitespace that can corrupt the rendered policy template.

Changes:

  • Trim leading/trailing whitespace (including newlines) from the cleaned AppArmor profile name after removing the " (enforce)" suffix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread contrib/apparmor/template.go Outdated
With testify, expected values go to the left.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the apparmor_fix_whitespace branch from c8bb2f8 to a419671 Compare April 23, 2026 19:02
Copilot AI review requested due to automatic review settings April 23, 2026 19:03
@thaJeztah thaJeztah force-pushed the apparmor_fix_whitespace branch from a419671 to f456e92 Compare April 23, 2026 19:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@thaJeztah thaJeztah force-pushed the apparmor_fix_whitespace branch from f456e92 to 1c89c8c Compare April 23, 2026 19:05
@thaJeztah thaJeztah added cherry-pick/1.7.x Change to be cherry picked to release/1.7 branch cherry-pick/2.0.x Change to be cherry picked to release/2.0 branch cherry-pick/2.1.x Change to be cherry picked to release/2.1 branch cherry-pick/2.2.x Change to be cherry picked to release/2.2 branch labels Apr 23, 2026
@thaJeztah thaJeztah marked this pull request as draft April 24, 2026 14:58
Copilot AI review requested due to automatic review settings April 24, 2026 16:02
@thaJeztah thaJeztah force-pushed the apparmor_fix_whitespace branch from 1c89c8c to a1fc501 Compare April 24, 2026 16:02
@thaJeztah thaJeztah changed the title contrib/apparmor: cleanProfileName: trim whitespace contrib/apparmor: fix whitespace handling in profile names Apr 24, 2026
@thaJeztah thaJeztah marked this pull request as ready for review April 24, 2026 16:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@thaJeztah thaJeztah force-pushed the apparmor_fix_whitespace branch from a1fc501 to 6ca7f8a Compare April 24, 2026 16:24
Copilot AI review requested due to automatic review settings April 24, 2026 16:25
@thaJeztah thaJeztah force-pushed the apparmor_fix_whitespace branch from 6ca7f8a to d0325a7 Compare April 24, 2026 16:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread contrib/apparmor/template.go
Comment thread contrib/apparmor/template.go
Comment thread contrib/apparmor/template.go
Comment on lines +198 to 205
for scanner.Scan() {
// Entries are of the form "<profile> (<mode>)", e.g. "foo (enforce)".
// Profile names may contain spaces (quoted names are supported in AppArmor),
// so split on " (" rather than the first space.
if prefix, _, ok := strings.Cut(scanner.Text(), " ("); ok && prefix == name {
return true, nil
}
}
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

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

The updated parsing logic in isLoaded (handling profile names with spaces by splitting on " (") is not currently unit-tested. To prevent regressions, consider extracting the line-matching logic into a helper that accepts an io.Reader (or a []string of lines) and add tests that cover names with spaces and the no-trailing-newline case.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, we should just use the moby implementation of this whole package (once I reconciled the remaining bits); it has coverage for this.

The profile we read from /proc/self/attr/current will contain a newline,
resulting in a stray newline to be added to the profile;

    Diff:
    --- Expected
    +++ Actual
    @@ -16,3 +16,4 @@
       # Manager may send signals to container processes.
    -  signal (receive) peer=unconfined,
    +  signal (receive) peer=unconfined
    +,
       # Container processes may send signals amongst themselves.

Trim whitespace to account for newlines and fix handling of whitespace as
AppArmor profile names are allowed to contain spaces when quoted; from the
[apparmor.d(5)] man-page:

    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.)

While we don't use those names in our code, let's make the code correct.

Also update the template to use quotes.

[apparmor.d(5)]: https://manpages.ubuntu.com/manpages/xenial/man5/apparmor.d.5.html

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
// Profile names may contain spaces, so split on " (" rather than the
// first space. Trim whitespace first because the value includes a
// trailing newline.
profile, _, _ = strings.Cut(strings.TrimSpace(profile), " (")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm I think it would be safer to go from the end - find the LAST ) and then find a matching ( before it.

Technically the profile name could also include parents.

That's also what the libapparmor does:
https://gitlab.com/apparmor/apparmor/-/blob/master/libraries/libapparmor/src/kernel.c?ref_type=heads#L578-615

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

Labels

cherry-pick/1.7.x Change to be cherry picked to release/1.7 branch cherry-pick/2.0.x Change to be cherry picked to release/2.0 branch cherry-pick/2.1.x Change to be cherry picked to release/2.1 branch cherry-pick/2.2.x Change to be cherry picked to release/2.2 branch kind/bug size/M

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

4 participants