[PATCH] Fix failure to remove an empty directory without read permission

Ziyao ziyao at disroot.org
Thu Feb 2 04:55:23 UTC 2023


Hi list,

The applet rm fails to remove an empty directoy without read permission 
and
just exits silently.

For example,
$ mkdir t && chmod 100 t
$ rm -rf t                 # Print nothing and the directory is still 
there
$ echo $?
1

This problem does not exist in GNU coreutils.

rm invokes remove_file() to do the actual remove, which tries to open it
first when removing a directory (libbb/remove_file.c:49). I guess it is 
for
removing its contents because we cannot remove a nonempty directory.
But if reading the directory is not permitted, opendir() will fail and 
cause
rm exiting without an error message.

This small patch tries to remove the directory first instead of opening 
it,
and prints a friendly message when the opening fails instead of exiting
silently.

----
diff --git a/libbb/remove_file.c b/libbb/remove_file.c
index 1505e62..7bee9f0 100644
--- a/libbb/remove_file.c
+++ b/libbb/remove_file.c
@@ -45,8 +45,15 @@ int FAST_FUNC remove_file(const char *path, int 
flags)
                                 return 0;
                 }

+               /*
+                *      Handle empty directoires even if without read 
permission
+                */
+               if (!rmdir(path))
+                       return 0;
+
                 dp = opendir(path);
-               if (dp == NULL) {
+               if (!dp) {
+                       bb_perror_msg("can't remove '%s'",path);
                         return -1;
                 }


-- 
Ziyao


More information about the busybox mailing list