]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
fix: memory leak of nsgrp in src/userdel.c update_groups()
authorArtem Semenov <savoptik@altlinux.org>
Mon, 8 Jun 2026 11:18:59 +0000 (14:18 +0300)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Thu, 6 Aug 2026 13:53:48 +0000 (15:53 +0200)
update_groups() duplicates each shadow group with __sgr_dup() to edit
its member list, but never releases the copy.  The 'nsgrp' struct sgrp,
along with its deep-copied name, passwd and member/admin arrays, leaks
once per iteration.

__sgr_dup() is not malloc()-like, so free() would not release the inner
allocations; use the matching sgr_free().

valgrind --leak-check=full --show-leak-kinds=all src/userdel -P <prefix> bob
(bob is a member of 10 groups), before this commit:

    551 (320 direct, 231 indirect) bytes in 10 blocks are definitely lost
       at 0x4848EB8: calloc
       by __sgr_dup (sgroupio.c:37)
       by update_groups (userdel.c:255)
    ...
    definitely lost: 1,120 bytes in 50 blocks

after this commit:

    definitely lost: 800 bytes in 40 blocks

Signed-off-by: Artem Semenov <savoptik@altlinux.org>
Reviewed-by: Alejandro Colomar <alx@kernel.org>
src/userdel.c

index 20f28281c13a90325b672ab7dd8458b27ae8c20b..5e7cf38eb48fc4ebefc31827ed389b7dc2b37b5d 100644 (file)
@@ -279,6 +279,7 @@ static void update_groups (bool process_selinux)
 #endif                         /* WITH_AUDIT */
                SYSLOG(LOG_INFO, "delete '%s' from shadow group '%s'\n",
                       user_name, nsgrp->sg_namp);
+               sgr_free (nsgrp);
        }
 #endif                         /* SHADOWGRP */
 }