Skip to content

apparmor: add signal and ptrace rules for stacked profiles - #12887

Open
lexfrei wants to merge 1 commit into
containerd:mainfrom
lexfrei:fix/apparmor-stacking-signal-ptrace
Open

lexfrei wants to merge 1 commit into
containerd:mainfrom
lexfrei:fix/apparmor-stacking-signal-ptrace

Conversation

@lexfrei

@lexfrei lexfrei commented Feb 10, 2026 •

Copy link
Copy Markdown

On kernel 6.17 with AppArmor profile stacking enabled (/sys/kernel/security/apparmor/features/domain/stack = yes), processes created via CRI ExecSync (kubectl exec, exec-based probes) run under a stacked profile cri-containerd.apparmor.d//&unconfined:

$ kubectl exec <pod> -- cat /proc/1/attr/apparmor/current
cri-containerd.apparmor.d (enforce)

$ kubectl exec <pod> -- cat /proc/self/attr/apparmor/current
cri-containerd.apparmor.d//&unconfined (enforce)

The existing signal and ptrace rules use peer={{.Name}}, which expands to cri-containerd.apparmor.d and does not match the stacked peer name cri-containerd.apparmor.d//&unconfined. This causes AppArmor DENIED audit events when exec'd Go processes send SIGURG for goroutine preemption:

audit: type=1400 apparmor="DENIED" operation="signal" profile="cri-containerd.apparmor.d"
  comm="bao" requested_mask="send" denied_mask="send" signal=urg
  peer="cri-containerd.apparmor.d//&unconfined"

This change adds rules with peer={{.Name}}//&* to match stacked profile peers for both signal and ptrace operations. The //&* glob matches any stacked profile suffix (e.g., //&unconfined).

Tested on Ubuntu 25.10 (questing) aarch64 with kernel 6.17.0-1007-raspi, AppArmor parser 5.0.0~alpha1, containerd v2.1.5-k3s1. The modified profile was loaded via apparmor_parser --replace. After loading, zero new DENIED entries over 56+ seconds (11+ probe cycles at 5s interval), whereas before the fix there were continuous denials every cycle.

Note: the abi <abi/3.0> pin from #12864 is already on main and does not resolve this issue.

Fixes: #12886

Why the rule is shaped this way

A flat peer=<profile> rule does not match a stacked peer label. The kernel matches the peer label as a single string: label_compound_match() walks the components and inserts the literal //& between them, so a rule whose text is cri-containerd.apparmor.d never matches cri-containerd.apparmor.d//&unconfined. The component fallback in label_components_match() does not rescue it either. It intersects permissions across components (accum->allow &= addend->allow & ~addend->deny), so Name contributes send,receive, unconfined contributes receive, and the intersection drops send. That is the SIGURG symptom in the report.

** rather than *, because signal.cc and ptrace.cc compile the peer label through convert_aaregex_to_pcre(peer_label, 0, glob_default, ...), where * becomes [^/\x00]* and ** becomes [^\x00]*. A single * stops at /. That leaves out path-named profiles and any stack deeper than two components, since the tail then contains a /.

Three positions, because the components of a stacked label are sorted lexically rather than by stacking order (profile_cmp() in security/apparmor/label.c). This profile can end up at the head, at the tail, or in the middle.

The rules do not widen privileges. A stacked label is an intersection of confinements: a task labelled A//&B is confined by both A and B, so its permissions are a subset of what A alone allows. A peer carrying this profile as a component is already inside the set that the existing peer={{.Name}} rule grants in full. The literal //& in the pattern also keeps hat profiles (parent//child, no &) out of the match.

Checked on a live kernel, Ubuntu 24.04, kernel 6.8, domain/stack=yes, AppArmor 4.0.1, entering the stacked label with aa-exec and running kill -9 on an own child. <profile> below is the generated profile name, cri-containerd.apparmor.d by default:

peer label rules in the profile result
<profile>//&unconfined flat peer={{.Name}} only denied
<profile>//&unconfined flat plus //&* allowed
<profile>//&other/sub//&unconfined flat plus //&* denied
<profile>//&other/sub//&unconfined flat plus //&** allowed
<profile>//&zzz-peer head-anchored //&** only allowed
aaa-peer//&<profile> head-anchored //&** only denied
<profile>//&zzz-peer all three positions allowed
aaa-peer//&<profile> all three positions allowed

The last two rows ran against the profile this template generates, not a hand-written stand-in.

The reported case looked like it needed only one rule because c sorts before u, so cri-containerd.apparmor.d happens to precede unconfined in the label. A profile named with a path always sorts first, / being 0x2f, and would not have been covered.

There is a narrower one-line alternative, relaxing signal (receive) peer=unconfined to (send,receive) so the component fallback covers the stack. That works, but it grants against every peer named unconfined, host processes included. The stacked-label form stays inside this profile's own stack.

The generated profile compiles on apparmor_parser 2.12, 2.13.3, 2.13.6, 3.0.4 and 4.0.1 when given a current kernel feature set, with the new rules present in the compiled policy rather than dropped. I checked that by compiling with and without them and comparing the cache, using removal of an ordinary file rule as a control to confirm the comparison detects a change at all. & is not an AARE metacharacter, so older parsers treat //& as a literal and need no awareness of stacking. One caveat if you reproduce this: without a feature set, in a container with no AppArmor, 3.0.4 and 4.0.1 print signal rules not enforced, drop those rules, and still exit 0, so a bare parse check in that setup proves less than it appears to.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Feb 10, 2026
@lexfrei
lexfrei force-pushed the fix/apparmor-stacking-signal-ptrace branch from 0318798 to b65590b Compare February 10, 2026 23:45
Copilot AI lite review requested due to automatic review settings June 12, 2026 16:22

Copilot AI left a comment

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds AppArmor “stacking mode” allowances so stacked profiles (e.g., name//&unconfined) can still signal/ptrace peer processes, and introduces a regression test to ensure the generated profile includes these rules.

Changes:

  • Extend the generated AppArmor profile template with //&* peer rules for signal and ptrace.
  • Add a Go test that renders the template and asserts the stacking rules are present.

Reviewed changes

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

File Description
contrib/apparmor/template.go Adds AppArmor template rules permitting signal/ptrace to stacked peer profiles (//&*).
contrib/apparmor/template_test.go Adds a template-generation test that asserts stacking rules are included in output.

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

Comment thread contrib/apparmor/template_test.go Resolved
Comment thread contrib/apparmor/template.go Resolved

Copilot AI left a comment

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.

Pull request overview

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

@thaJeztah

Copy link
Copy Markdown
Member

On kernel 6.17 with AppArmor profile stacking enabled (/sys/kernel/security/apparmor/features/domain/stack = yes), processes created via CRI ExecSync (kubectl exec, exec-based probes) run under a stacked profile cri-containerd.apparmor.d//&unconfined:

Any compatibility issues with this change on older kernels? (just double checking; I recall we've had some at some point)

@lexfrei

lexfrei commented Jul 16, 2026

Copy link
Copy Markdown
Author

@thaJeztah no. They're plain AARE peer patterns on the same rule types already in the template a couple of lines up, so there's nothing new for the parser to understand — apparmor_parser -QT takes them on 2.12, 2.13.3, 3.0.4 and 4.0.1, and the first two predate stacking entirely. On a kernel without stacking the pattern matches no label, and a non-matching allow rule grants nothing, so behaviour there is unchanged.

The breakages you're thinking of were in version/ABI detection rather than rules: #9464 (parsing apparmor_parser --version broke on Ubuntu 23.10 — which is why those checks came out) and #12726 behind the abi/3.0 pin in #12864. The only version-sensitive line in the template is still abi <abi/3.0>, — it does fail on 2.12 with Found unexpected character: '<', but loadData only emits it when /etc/apparmor.d/abi/3.0 exists, so old distros never get it.

Copilot AI review requested due to automatic review settings July 16, 2026 12:36
@lexfrei
lexfrei force-pushed the fix/apparmor-stacking-signal-ptrace branch from 56af135 to 16f6908 Compare July 16, 2026 12:36

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread contrib/apparmor/template_test.go Resolved
Copilot AI review requested due to automatic review settings July 16, 2026 12:51
@lexfrei
lexfrei force-pushed the fix/apparmor-stacking-signal-ptrace branch from 16f6908 to d9ba8c6 Compare July 16, 2026 12:51

Copilot AI left a comment

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.

Pull request overview

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

Copilot AI review requested due to automatic review settings August 14, 2026 08:33
@lexfrei
lexfrei force-pushed the fix/apparmor-stacking-signal-ptrace branch from d9ba8c6 to 96dbd3b Compare August 14, 2026 08:33

Copilot AI left a comment

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.

Pull request overview

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

Copilot AI review requested due to automatic review settings August 14, 2026 08:42
@lexfrei
lexfrei force-pushed the fix/apparmor-stacking-signal-ptrace branch from 96dbd3b to 20cc0d6 Compare August 14, 2026 08:42

Copilot AI left a comment

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.

Pull request overview

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

On kernel 6.17 with AppArmor profile stacking enabled, processes
created via CRI ExecSync (kubectl exec, exec-based probes) run under
a stacked profile like "cri-containerd.apparmor.d//&unconfined"
instead of plain "cri-containerd.apparmor.d".

The existing signal and ptrace rules use peer={{.Name}} which does not
match the stacked peer name, causing AppArmor DENIED audit events for
SIGURG (Go runtime goroutine preemption) and potentially ptrace
operations from exec'd processes.

Add signal and ptrace rules that match the stacked label. Its components
are ordered lexically rather than by stacking order, so this profile can
appear at either end or in the middle of the label, and each position
needs its own rule. The glob is ** rather than *, because * stops at "/"
while both profile names and the remaining components can contain one.

Stacking only ever narrows privileges, since a task carrying a stacked
label is confined by every profile in it. A peer whose label contains
this profile is therefore already within the peer set the pre-existing
peer={{.Name}} rule grants in full.

Tested by loading the modified profile via apparmor_parser --replace
on Ubuntu 25.10 aarch64, kernel 6.17.0-1007-raspi, AppArmor parser
5.0.0~alpha1. After loading, zero DENIED entries over 56+ seconds
(11+ exec probe cycles), whereas before there were continuous denials.

Fixes: containerd#12886

Assisted-By: Claude <[email protected]>
Signed-off-by: Aleksei Sviridkin <[email protected]>
Copilot AI review requested due to automatic review settings August 14, 2026 09:07
@lexfrei
lexfrei force-pushed the fix/apparmor-stacking-signal-ptrace branch from 20cc0d6 to 24f370f Compare August 14, 2026 09:07
@kubernetes-prow kubernetes-prow Bot added size/M and removed size/S labels Aug 14, 2026

Copilot AI left a comment

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.

Pull request overview

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

@lexfrei

lexfrei commented Aug 14, 2026

Copy link
Copy Markdown
Author

#13905 fixes the same thing, found independently. The difference is coverage: the literal peer="<profile>//&unconfined" covers the case in the report, the glob also covers stacks deeper than two components, stacks with something other than unconfined on top, and the reverse component order. Components of a stacked label sort lexically rather than by stacking order, so a path-named profile sorts first and pushes this profile to the tail. Checked on a live kernel, the table is in the description.

Happy to consolidate either way, including closing this one, as long as the rule that lands is the glob.

Two things I ran into that don't belong here. The NOTE about keeping in sync with the docker profile: moby's template has no stacked-label rules at all, so there is nothing to mirror mechanically, and how you want that handled is your call. And nothing checks that the generated profile parses, TestDumpDefaultProfile gates on apparmor_parser --version and then never runs it on the profile. I can send that one separately if it is useful.

The red required check is not this change. Report required job statuses has no failure of its own, its step is run: exit 1 and it fires when a watched job fails, so it is downstream of E2E / Kubernetes Node. E2E ran 536 specs, 535 passed, and the single failure is UsageNanoCores reading 1000189917 against a bounded(10000, 1e9) ceiling, over by 0.019%. Those pods run dd if=/dev/zero of=/dev/null bs=1 count=10000000, so the workload saturates a core by design while the ceiling sits at exactly one core, and the same file bounds the system-container case at 2e10 with a comment that the CRI value is an estimate. These rules mediate signal and ptrace, the assertion measures CPU accounting, and apparmor appears nowhere in the job log. Could you re-run that job when convenient? I can't trigger it from a fork.

@kubernetes-prow

Copy link
Copy Markdown

PR needs rebase.

Details

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-sigs/prow repository.

This branch has not been deployed

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

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

AppArmor: signal rule does not match stacked profile peer on kernel 6.17

4 participants