[PATCH] apparmor: set xattr_count only after the xattrs array is allocated

From: Cong Nguyen

Date: Fri Nov 27 2026 - 03:04:14 EST


unpack_xattrs() sets profile->attach.xattr_count before it allocates
profile->attach.xattrs with kcalloc(). If the allocation fails, the load
fails and free_attachment() runs on the profile. It loops over
xattr_count entries and reads attach->xattrs[i], which is NULL, so it
dereferences a NULL pointer.

Set xattr_count after the allocation has succeeded.

Fixes: 8e51f9087f40 ("apparmor: Add support for attaching profiles via xattr, presence and value")
Signed-off-by: Cong Nguyen <congnt264@xxxxxxxxx>
---
security/apparmor/policy_unpack.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/security/apparmor/policy_unpack.c b/security/apparmor/policy_unpack.c
index 24fbce2e4322..83cfa981f7bd 100644
--- a/security/apparmor/policy_unpack.c
+++ b/security/apparmor/policy_unpack.c
@@ -598,10 +598,10 @@ static bool unpack_xattrs(struct aa_ext *e, struct aa_profile *profile)

if (!aa_unpack_array(e, NULL, &size))
goto fail;
- profile->attach.xattr_count = size;
profile->attach.xattrs = kcalloc(size, sizeof(char *), GFP_KERNEL);
if (!profile->attach.xattrs)
goto fail;
+ profile->attach.xattr_count = size;
for (i = 0; i < size; i++) {
if (!aa_unpack_strdup(e, &profile->attach.xattrs[i], NULL))
goto fail;
--
2.25.1