Skip to content

utils: fix apparmor profile not applied in user namespaces#2065

Merged
giuseppe merged 1 commit intocontainers:mainfrom
giuseppe:fix-apparmour-with-userns
Apr 2, 2026
Merged

utils: fix apparmor profile not applied in user namespaces#2065
giuseppe merged 1 commit intocontainers:mainfrom
giuseppe:fix-apparmour-with-userns

Conversation

@giuseppe
Copy link
Copy Markdown
Member

@giuseppe giuseppe commented Apr 2, 2026

The refactoring in commit 0a3e929 ("utils: fix memory leak and missing cache in libcrun_initialize_apparmor()") introduced a regression where AppArmor profiles were silently not applied when user namespaces with UID/GID mappings were used.

The issue was in libcrun_initialize_apparmor(): when crun_dir_p_at() fails to stat /sys/kernel/security/apparmor (which is expected inside a user namespace where securityfs may not be accessible), the refactored code immediately concluded AppArmor was disabled and returned 0.

Fix it by dropping the /sys/kernel/security/apparmor directory check and only using /sys/module/apparmor/parameters/enabled. Only ENOENT on the open indicates AppArmor is not available; any other error is reported to the caller.

Add a test that verifies AppArmor profiles are correctly applied when running with a user namespace and UID/GID mappings.

Closes: #2063

@giuseppe
Copy link
Copy Markdown
Member Author

giuseppe commented Apr 2, 2026

@jnovy PTAL

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors libcrun_initialize_apparmor to improve AppArmor detection within user namespaces by directly checking /sys/module/apparmor/parameters/enabled instead of relying on securityfs. It also adds a regression test, test_process_apparmor_profile_userns, to verify profile application when UID/GID mappings are present. Feedback was provided to remove an unnecessary --raw flag from the apparmor_parser command used for test cleanup to ensure correct behavior across different versions of the tool.

Comment on lines +310 to +311
subprocess.run(["apparmor_parser", "--remove", "--raw"],
input=profile_text.encode(), capture_output=True)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The --raw flag in apparmor_parser is used to dump the binary representation of a profile to stdout. It is not intended for use with the --remove (or -R) action. While it might be ignored by some versions of the parser, it's better to remove it to avoid confusion and ensure the command behaves as expected.

Suggested change
subprocess.run(["apparmor_parser", "--remove", "--raw"],
input=profile_text.encode(), capture_output=True)
subprocess.run(["apparmor_parser", "--remove"],
input=profile_text.encode(), capture_output=True)

The refactoring in commit 0a3e929 ("utils: fix memory leak and missing
cache in libcrun_initialize_apparmor()") introduced a regression where
AppArmor profiles were silently not applied when user namespaces with
UID/GID mappings were used.

The issue was in libcrun_initialize_apparmor(): when crun_dir_p_at()
fails to stat /sys/kernel/security/apparmor (which is expected inside a
user namespace where securityfs may not be accessible), the refactored
code immediately concluded AppArmor was disabled and returned 0.

Fix it by dropping the /sys/kernel/security/apparmor directory check
and only using /sys/module/apparmor/parameters/enabled.  Only ENOENT
on the open indicates AppArmor is not available; any other error is
reported to the caller.

Add a test that verifies AppArmor profiles are correctly applied when
running with a user namespace and UID/GID mappings.

Closes: containers#2063

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
@giuseppe giuseppe force-pushed the fix-apparmour-with-userns branch from abf105b to 94d7f64 Compare April 2, 2026 10:10
@jnovy
Copy link
Copy Markdown
Contributor

jnovy commented Apr 2, 2026

Thanks, LGTM

@giuseppe giuseppe merged commit 0097db1 into containers:main Apr 2, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crun v1.27 with --uidmap/--gidmap does not set apparmor profile

2 participants