From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796387518; b=UVFp6K3Rin8vxsEU7TsUWSufGYaXuGXvjMY1Kn5k484zqz6KkVevxLZNfQYgRg1pkVdF I9FiO6xLrPMuZ34bDO0y4J+CEc4YsAzC2yzYBb8DB9BhMwy2s3TMKtwmbbejneOfV5quC A5XhS7SWop6EEXX8jVu79R9IB/FWFQWiA95zPi1c1/CUa1UE0jiOmnampbRKCRLg2qA+Q 3bOS3Ox1isRG3HXaFjiuGw0wp97XbPLMu5IoHsE59SQm4JKbKWDwAZgiYClHv79+t+l8v n4JH4dEmNvFBe91OEB4YcU9FVWEZcYadf/Uu+Y6L0KJJLFAZbLELx8ZI8OKoL9VlzNg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387518; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=OuOIujAJUCQqIhjRHMjkpTN7SkKNDu9c1IRHJDTSUJU=; b=k51MvmDz5lPeRQhIgcNhHAww678VftepRjp+SWBz4mMxU/MmTStB1uf5LErVTVE+rsGx 4VJugMEw0kJjJgQTRXzJYlzow3wTbM0RQZqD2auWPJwmIpmipsriamA/rd2e4vJMQrAnx Ja1zwJIBCL3fYzywJEbHdrq+v+NehmgtBDr5f98pTOL7uo+ATXhK7UgI8KMbnJa96sGLw OdU8q1U/XWye6TKavO30FMoGirRQbrJW8YhHI5r4bwpL8WY1zHHtit2LaNMZHmxr23mm6 8rDFtZPpfBXIfqZ7uqR7vPhiuv4g7H4RBZunBza+dCPZjpda0UDjUnIN8JZmC4/1OAA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=vgXB5mHk; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F09B1606A322; Fri, 4 Dec 2026 12:31:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=vgXB5mHk Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C7971F000CE; Fri, 4 Dec 2026 12:31:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387517; bh=OuOIujAJUCQqIhjRHMjkpTN7SkKNDu9c1IRHJDTSUJU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=vgXB5mHkIr//4XvM0SakdgdDEt7+eLt4NOMWd/Q9rG+sIW7HTgqGk2qznbRYT525L fnCrXy1kv98WU86C9rz5mNdPjbbl41yBHpfZwL+cQ2YvtbLkwZDKNvZTSbaYvpWX/z 1+Pwpbk7CF10rPT2C+Rx+GWdntZS+2eXcnqq757w= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Georgia Garcia , John Johansen , Sasha Levin Subject: [PATCH 6.1 0237/1059] apparmor: fix profile leak when a policy load fails Date: Fri, 4 Dec 2026 18:56:08 +0900 Message-ID: <20261204100650.410494179@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Georgia Garcia [ Upstream commit ce94927fa4173fcc1eca27bdc65a140064c8f1e6 ] The error paths of aa_replace_profiles() and aa_unpack() do not break the refcount cycle between label and proxy. They just aa_put_profile() the new profiles, which drops the label refcount from 2 to 1, and the proxy keeps the profile alive. Every failed atomic policy load (e.g. "profile can not be replaced", or a later profile in the blob failing to unpack) leaks every new profile in the set, along with its proxy, rules and its loaddata reference. The leak is usually not visible from userspace, because the raw_data directory for a failed load is not created, but it becomes visible when the blob is deduplicated onto an existing loaddata, which then keeps its policy/raw_data/ directory forever. Add aa_put_unpublished_profile(), which drops the profile's own proxy before putting the profile, and use it on those error paths. The proxy of an unpacked profile is always freshly allocated and not referenced by anything else before the profile is published, so putting it is safe. Fixes: 637f688dc3dc ("apparmor: switch from profiles to using labels on contexts") Cc: stable@vger.kernel.org Signed-off-by: Georgia Garcia Signed-off-by: John Johansen [ sashal: Context adjusted for the older tree: AUDIT_MODE takes a non-const struct aa_profile *, and fail_profile: and fail: lack upstream's kfree(ns_name) and compressed_data lines; the added code is otherwise identical. ] Signed-off-by: Sasha Levin --- security/apparmor/include/policy.h | 20 ++++++++++++++++++++ security/apparmor/policy.c | 3 +++ security/apparmor/policy_unpack.c | 4 +++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/security/apparmor/include/policy.h b/security/apparmor/include/policy.h index ae8a3626ac75a..94cfb6f87a8ce 100644 --- a/security/apparmor/include/policy.h +++ b/security/apparmor/include/policy.h @@ -299,6 +299,26 @@ static inline void aa_put_profile(struct aa_profile *p) kref_put(&p->label.count.count, aa_label_kref); } +/** + * aa_put_unpublished_profile - put a profile that was never made visible + * @p: profile (MAYBE NULL) + * + * A newly allocated profile and its proxy reference each other + * (proxy->label holds a label refcount). That cycle is normally broken + * when the profile is replaced or removed, which redirects the proxy. + * A profile that is discarded before being added to the policy tree + * never goes through either path, so break the cycle here, otherwise + * the profile, its proxy and its rawdata are leaked. + */ +static inline void aa_put_unpublished_profile(struct aa_profile *p) +{ + if (p && p->label.proxy) { + aa_put_proxy(p->label.proxy); + p->label.proxy = NULL; + } + aa_put_profile(p); +} + static inline int AUDIT_MODE(struct aa_profile *profile) { if (aa_g_audit != AUDIT_NORMAL) diff --git a/security/apparmor/policy.c b/security/apparmor/policy.c index dac3fb515814d..586d70ae2b75b 100644 --- a/security/apparmor/policy.c +++ b/security/apparmor/policy.c @@ -1212,6 +1212,9 @@ ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label, } list_for_each_entry_safe(ent, tmp, &lh, list) { list_del_init(&ent->list); + /* new was never added to the policy tree */ + aa_put_unpublished_profile(ent->new); + ent->new = NULL; aa_load_ent_free(ent); } diff --git a/security/apparmor/policy_unpack.c b/security/apparmor/policy_unpack.c index 636fa078f00e6..ae98cd22a5b55 100644 --- a/security/apparmor/policy_unpack.c +++ b/security/apparmor/policy_unpack.c @@ -1261,11 +1261,13 @@ int aa_unpack(struct aa_loaddata *udata, struct list_head *lh, return 0; fail_profile: - aa_put_profile(profile); + aa_put_unpublished_profile(profile); fail: list_for_each_entry_safe(ent, tmp, lh, list) { list_del_init(&ent->list); + aa_put_unpublished_profile(ent->new); + ent->new = NULL; aa_load_ent_free(ent); } -- 2.53.0