shared/rm-rf: loop over nested directories instead of instead of recursing
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 30 Nov 2021 21:29:05 +0000 (22:29 +0100)
committerMichael Biebl <biebl@debian.org>
Sun, 7 Aug 2022 13:25:09 +0000 (14:25 +0100)
commitdbdc762ddb0ca9de806e4da99c87c37adc466a74
tree20a76497caca6cedee5604a0bab2143a9a2b7d10
parent5b7f1c25f5f119aa3fd59a583f104e644a031213
shared/rm-rf: loop over nested directories instead of instead of recursing

To remove directory structures, we need to remove the innermost items first,
and then recursively remove higher-level directories. We would recursively
descend into directories and invoke rm_rf_children and rm_rm_children_inner.
This is problematic when too many directories are nested.

Instead, let's create a "TODO" queue. In the the queue, for each level we
hold the DIR* object we were working on, and the name of the directory. This
allows us to leave a partially-processed directory, and restart the removal
loop one level down. When done with the inner directory, we use the name to
unlinkat() it from the parent, and proceed with the removal of other items.

Because the nesting is increased by one level, it is best to view this patch
with -b/--ignore-space-change.

This fixes CVE-2021-3997, https://bugzilla.redhat.com/show_bug.cgi?id=2024639.
The issue was reported and patches reviewed by Qualys Team.
Mauro Matteo Cascella and Riccardo Schirone from Red Hat handled the disclosure.

(cherry picked from commit 5b1cf7a9be37e20133c0208005274ce4a5b5c6a1)
(cherry picked from commit 911516e1614e435755814ada5fc6064fa107a105)
(cherry picked from commit 6a28f8b55904c818b25e4db2e1511faac79fd471)
(cherry picked from commit c752f27b7647c99b4a17477c99d84fd8c950ddf0)
(cherry picked from commit 921810ea23357988ce67f49190f43abef1788a9c)

Gbp-Pq: Name shared-rm-rf-loop-over-nested-directories-instead-of-inst.patch
src/basic/rm-rf.c