apparmor: fix profile leak when a policy load fails

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/<rev> 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: 637f688dc3 ("apparmor: switch from profiles to using labels on contexts")
Cc: stable@vger.kernel.org
Signed-off-by: Georgia Garcia <georgia.garcia@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
This commit is contained in:
Georgia Garcia 2026-10-14 19:08:21 -03:00 committed by John Johansen
parent 97ba44a2aa
commit ce94927fa4
3 changed files with 26 additions and 1 deletions

View File

@ -436,6 +436,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(const struct aa_profile *profile)
{
if (aa_g_audit != AUDIT_NORMAL)

View File

@ -1462,6 +1462,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);
}

View File

@ -1822,13 +1822,15 @@ int aa_unpack(struct aa_loaddata *udata, struct list_head *lh,
fail_profile:
kfree(ns_name);
aa_put_profile(profile);
aa_put_unpublished_profile(profile);
fail:
if (compressed_data)
kvfree(compressed_data);
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);
}