Linux Security Modules development
 help / color / mirror / Atom feed
From: Andrew Kroh <andrew.kroh@xata.io>
To: John Johansen <john.johansen@canonical.com>,
	Georgia Garcia <georgia.garcia@canonical.com>
Cc: apparmor@lists.ubuntu.com, linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] apparmor: fix label replacement compiled out of __label_insert()
Date: Thu, 19 Nov 2026 15:36:50 -0500	[thread overview]
Message-ID: <20261119203650.51147-1-andrew.kroh@xata.io> (raw)

__label_insert() replaces an equal label in the labelset from inside
an assertion.

	AA_BUG(!__label_replace(this, label));

AA_BUG() evaluates its argument only if
CONFIG_SECURITY_APPARMOR_DEBUG_ASSERTS is set. That option depends on
CONFIG_SECURITY_APPARMOR_DEBUG, which is off by default. Without it,
the compiler discards the call to __label_replace(). The new label
does not go into the tree and does not get FLAG_IN_TREE. The caller,
__label_update(), then removes the old label from the tree.

The proxy of the old label points to the new label. A task that
holds the old label therefore moves to the new label after the first
profile replacement. The new label is not in the labelset, so
__labelset_update() cannot find it on later replacements. A task
with a compound (stacked) label keeps the profile versions from the
first replacement and ignores each later replacement until it execs.

Containers that set no_new_privs and change confinement with a stack
transition show the problem. To reproduce it, load two profiles.

  profile a { ... /usr/bin/bash Px -> a//&b, }
  profile b { ... }

Start a long-lived bash under a//&b. Replace b with a version that
denies a file, then with a version that allows the file again. The
task continues to get the deny. A task under a plain profile follows
every replacement.

Call __label_replace() outside the assertion and assert on its
result.

Fixes: f1bd904175e8 ("apparmor: add the base fns() for domain labels")
Cc: stable@vger.kernel.org
Signed-off-by: Andrew Kroh <andrew.kroh@xata.io>
---
Reproduced on Ubuntu 6.8.0-149-generic, 7.0.0-43-generic and on
apparmor-next (7.4-rc1) on arm64. These steps show the bug (as root):

  echo 'profile a { file, capability, } profile b { file, capability, }' > /tmp/allow
  echo 'profile a { file, capability, } profile b { file, capability, deny /etc/hostname r, }' > /tmp/deny
  apparmor_parser -r /tmp/allow   # load a and b
  aa-exec -p 'a//&b' -- bash      # shell confined by the stack a//&b
  read x < /etc/hostname          # allowed
  apparmor_parser -r /tmp/deny    # replace b: deny /etc/hostname
  read x < /etc/hostname          # denied
  apparmor_parser -r /tmp/allow   # replace b: allow it again
  read x < /etc/hostname          # still denied; allowed with this patch

The check must come from the long-lived task itself. A new exec
(e.g. cat) gets a freshly built label and is not affected.

No other AA_BUG() in security/apparmor has a side effect in its
argument.

 security/apparmor/label.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/security/apparmor/label.c b/security/apparmor/label.c
index fd67262330f9..0dc18743b04a 100644
--- a/security/apparmor/label.c
+++ b/security/apparmor/label.c
@@ -693,6 +693,8 @@ static struct aa_label *__label_insert(struct aa_labelset *ls,
 
 		parent = *new;
 		if (result == 0) {
+			bool replaced;
+
 			/* !__aa_get_label means queued for destruction,
 			 * so replace in place, however the label has
 			 * died before the replacement so do not share
@@ -703,7 +705,8 @@ static struct aa_label *__label_insert(struct aa_labelset *ls,
 					return this;
 			} else
 				__proxy_share(this, label);
-			AA_BUG(!__label_replace(this, label));
+			replaced = __label_replace(this, label);
+			AA_BUG(!replaced);
 			return aa_get_label(label);
 		} else if (result < 0)
 			new = &((*new)->rb_left);

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.56.0


             reply	other threads:[~2026-11-19 20:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 20:36 Andrew Kroh [this message]
2026-11-19 20:42 ` [PATCH] apparmor: fix label replacement compiled out of __label_insert() sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119203650.51147-1-andrew.kroh@xata.io \
    --to=andrew.kroh@xata.io \
    --cc=apparmor@lists.ubuntu.com \
    --cc=georgia.garcia@canonical.com \
    --cc=john.johansen@canonical.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox